Lecture-07 Array
Lecture-07 Array
Visual Programming
• To create an array instance, you use the new keyword followed by the element
type, followed by the size of the array you’re creating between square brackets
Creating an array also initializes its elements by using the now familiar default
values (0, null, or false, depending on whether the type is numeric, a reference,
or a Boolean, respectively) For example, to create and initialize a new array of
four integers for the pins variable declared earlier, you write this:
Initializing Array Variables
• When you create an array instance, all the elements of the array instance are initialized to a
default value depending on their type.
• You can modify this behavior and initialize the elements of an array to specify values if you
prefer You achieve this by providing a comma separated list of values between a pair of
braces .
• For example, to initialize pins to an array of four int variables whose values are 9, 3, 7, and 2,
you write this:
Creating an Implicitly Typed Array
• The element type when you declare an array must match the type of elements that you at- tempt to
store in the array For example, if you declare pins to be an array of int, as shown in the preceding
examples, you cannot store a double, string, struct, or anything that is not an int in this array
• If you specify a list of initializers when declaring an array, you can let the C# compiler infer the
actual type of the elements in the array for you, like this:
• Note: In this example, the C# compiler determines that the names variable is an array of strings .
• you omit the square brackets from the type; the names variable in this example is declared simply as
var, and not var[].
• Second, you must specify the new operator and square brackets before the initializer list . If you use
this syntax, you must ensure that all the initializers have the same type.
Cont…
• Implicitly typed arrays are most useful when you are working with anonymous
types, described in later, “Creating and Managing Classes and Objects ”
• The following code creates an array of anonymous objects, each containing two
fields specifying the name and age of the members of family .
Accessing an Individual Array Element
• To access an individual array element, you must provide an index
indicating which element you require.
Iterating Through an Array
• Same as you are working in C++ or java.
Copying Arrays
• the CopyTo method copies the contents of one array into another array given a specifed starting
index:
• Another way to copy the values is to use the System.Array static method named Copy As with
CopyTo, you must initialize the target array before calling Copy:
• Yet another alternative is to use the System.Array instance method named Clone You can call this
method to create an entire array and copy it in one action:
Using Multidimensional Arrays
• you can visualize a two-dimensional array as a table, where the first
dimension specifies the number of rows and the second specifies the number
of columns.
• To access an element in the array, you provide two index values to specify
the “cell” holding the element
Understanding Parameter Arrays
• Parameter arrays are useful if you want to write methods that can
take any number of arguments, possibly of different types, as
parameters.
• Using Array Arguments
Declaring a params Array
• You use the params keyword as an array parameter modifer.