0% found this document useful (0 votes)
9 views

Lecture-07 Array

Uploaded by

kingofdeath1380
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture-07 Array

Uploaded by

kingofdeath1380
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Ghalib Private University

Visual Programming

By: M.Dawood Saddiqi


What Is an Array?
• An array is an unordered sequence of elements All the
elements in an array have the same type (unlike the fields in
a structure or class, which can have different types).
• The elements of an array live in a contiguous block of
memory and are accessed by using an integer index (unlike
fields in a structure or class, which are accessed by name).
Declaring Array Variables
• You declare an array variable by specifying the name of the element
type, followed by a pair of square brackets, followed by the variable
name The square brackets signify that the variable is an array
• For example, to declare an array of int variables named pins, you
write.
Creating an Array Instance

• 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.

• —this time with its array parameter declared as a params array.


• The effect of the params keyword on the Min method is that it allows you to
call it by using any number of integer arguments.
Cont…
• You can’t use the params keyword on multidimensional arrays .

• A params array must be the last parameter.

• A non-params method always takes priority over a params method


Using a params Array
• We will implement and test a method named Sum The
purpose of this method is to calculate the sum of a variable
number of int arguments passed to it, returning the result
as an int. We will do this by writing Sum to take a params
int[] parameter.

You might also like