arrays tutorial (c#)

4
 2/27/2015 Arrays Tutori al ( C#) https://msdn.microsoft.com/en-us/library/aa288453( d=printer,v=vs.71).aspx 1/4 Arrays Tutorial This tutorial describes arrays and shows how they work in C#. Sample Files See Arrays Sample to download and build the sample files discussed in this tutorial. Further Reading Arrays 12. Arrays foreach, in Collection Classes Tutorial Tutorial This tutorial is divided into the following sections: Arrays in General Declaring Arrays Initializing Arrays Accessing Array Members Arrays are Objects Using foreach with Arrays Arrays in General C# arrays are zero indexed; that is, the array indexes start at zero. Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences you should be aware of. When declaring an array, the square brackets []  must come after the type, not the identifier. Placing the brackets after the identifier is not legal syntax in C#. Another detail is that the size of the array is not part of its type as it is in the C language. This allows you to declare an array and assign any array of int objects to it, regardless of the array's length. Declaring Arrays C# supports singledimensional arrays, multidimensional arrays rectangular arrays  , and arrayofarrays jagged arrays  . The following examples show how to declare different kinds of arrays: Singledimensional arrays: Multidimensional arrays: Arrayofarrays jagged  : Declaring them as shown above  does not actually create the arrays. In C#, arrays are objects discussed later in this tutorial  and must be instantiated. The following examples show h to create arrays: Singledimensional arrays: Multidimensional arrays: Visual Studio .NET 2003 int[] table; // not int table[]; int[] numbers; // declare numbers as an int array of any size numbers = new int[10]; // numbers is a 10element array numbers = new int[20]; // now it's a 20element array int[] numbers; string[,] names; byte[][] scores; int[] numbers = new int[5];

Upload: abhishek-tripathi

Post on 02-Nov-2015

11 views

Category:

Documents


0 download

DESCRIPTION

dsvdsdsg

TRANSCRIPT

  • 2/27/2015 ArraysTutorial(C#)

    https://msdn.microsoft.com/enus/library/aa288453(d=printer,v=vs.71).aspx 1/4

    Arrays Tutorial

    This tutorial describes arrays and shows how they work in C#.

    Sample FilesSee Arrays Sample to download and build the sample files discussed in this tutorial.

    Further ReadingArrays12. Arraysforeach, inCollection Classes Tutorial

    TutorialThis tutorial is divided into the following sections:

    Arrays in GeneralDeclaring ArraysInitializing ArraysAccessing Array MembersArrays are ObjectsUsing foreach with Arrays

    Arrays in GeneralC# arrays are zero indexed; that is, the array indexes start at zero. Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences thatyou should be aware of.

    When declaring an array, the square brackets [] must come after the type, not the identifier. Placing the brackets after the identifier is not legal syntax in C#.

    Another detail is that the size of the array is not part of its type as it is in the C language. This allows you to declare an array and assign any array of int objects to it, regardless of thearray's length.

    Declaring ArraysC# supports singledimensional arrays, multidimensional arrays rectangular arrays, and arrayofarrays jagged arrays. The following examples show how to declare different kinds ofarrays:

    Singledimensional arrays:

    Multidimensional arrays:

    Arrayofarrays jagged:

    Declaring them as shown above does not actually create the arrays. In C#, arrays are objects discussed later in this tutorial and must be instantiated. The following examples show howto create arrays:

    Singledimensional arrays:

    Multidimensional arrays:

    Visual Studio .NET 2003

    int[]table;//notinttable[];

    int[]numbers;//declarenumbersasanintarrayofanysizenumbers=newint[10];//numbersisa10elementarraynumbers=newint[20];//nowit'sa20elementarray

    int[]numbers;

    string[,]names;

    byte[][]scores;

    int[]numbers=newint[5];

  • 2/27/2015 ArraysTutorial(C#)

    https://msdn.microsoft.com/enus/library/aa288453(d=printer,v=vs.71).aspx 2/4

    Arrayofarrays jagged:

    You can also have larger arrays. For example, you can have a threedimensional rectangular array:

    You can even mix rectangular and jagged arrays. For example, the following code declares a singledimensional array of threedimensional arrays of twodimensional arrays of type int

    ExampleThe following is a complete C# program that declares and instantiates arrays as discussed above.

    Output

    Initializing ArraysC# provides simple and straightforward ways to initialize arrays at declaration time by enclosing the initial values in curly braces {}. The following examples show different ways to initializedifferent kinds of arrays.

    NoteIf you do not initialize an array at the time of declaration, the array members are automatically initialized to the default initial value for the array type. Also, if youdeclare the array as a field of a type, it will be set to the default value null when you instantiate the type.

    SingleDimensional Array

    You can omit the size of the array, like this:

    string[,]names=newstring[5,4];

    byte[][]scores=newbyte[5][];for(intx=0;x

  • 2/27/2015 ArraysTutorial(C#)

    https://msdn.microsoft.com/enus/library/aa288453(d=printer,v=vs.71).aspx 3/4

    You can also omit the new operator if an initializer is provided, like this:

    Multidimensional Array

    You can omit the size of the array, like this:

    You can also omit the new operator if an initializer is provided, like this:

    Jagged Array ArrayofArraysYou can initialize jagged arrays like this example:

    You can also omit the size of the first array, like this:

    or

    Notice that there is no initialization syntax for the elements of a jagged array.

    Accessing Array MembersAccessing array members is straightforward and similar to how you access array members in C/C++. For example, the following code creates an array called numbers and then assigns a to the fifth element of the array:

    The following code declares a multidimensional array and assigns 5 to the member located at [1,1]:

    The following is a declaration of a singledimension jagged array that contains two elements. The first element is an array of two integers, and the second is an array of three integers:

    The following statements assign 58 to the first element of the first array and 667 to the second element of the second array:

    int[]numbers=newint[]{1,2,3,4,5};string[]names=newstring[]{"Matt","Joanne","Robert"};

    int[]numbers={1,2,3,4,5};string[]names={"Matt","Joanne","Robert"};

    int[,]numbers=newint[3,2]{{1,2},{3,4},{5,6}};string[,]siblings=newstring[2,2]{{"Mike","Amy"},{"Mary","Albert"}};

    int[,]numbers=newint[,]{{1,2},{3,4},{5,6}};string[,]siblings=newstring[,]{{"Mike","Amy"},{"Mary","Albert"}};

    int[,]numbers={{1,2},{3,4},{5,6}};string[,]siblings={{"Mike","Amy"},{"Mary","Albert"}};

    int[][]numbers=newint[2][]{newint[]{2,3,4},newint[]{5,6,7,8,9}};

    int[][]numbers=newint[][]{newint[]{2,3,4},newint[]{5,6,7,8,9}};

    int[][]numbers={newint[]{2,3,4},newint[]{5,6,7,8,9}};

    int[]numbers={10,9,8,7,6,5,4,3,2,1,0};numbers[4]=5;

    int[,]numbers={{1,2},{3,4},{5,6},{7,8},{9,10}};numbers[1,1]=5;

    int[][]numbers=newint[][]{newint[]{1,2},newint[]{3,4,5}};

    numbers[0][0]=58;

  • 2/27/2015 ArraysTutorial(C#)

    https://msdn.microsoft.com/enus/library/aa288453(d=printer,v=vs.71).aspx 4/4

    Arrays are ObjectsIn C#, arrays are actually objects. System.Array is the abstract base type of all array types. You can use the properties, and other class members, that System.Array has. An example of thiswould be using the Length property to get the length of an array. The following code assigns the length of the numbers array, which is 5, to a variable called LengthOfNumbers:

    The System.Array class provides many other useful methods/properties, such as methods for sorting, searching, and copying arrays.

    Using foreach on ArraysC# also provides the foreach statement. This statement provides a simple, clean way to iterate through the elements of an array. For example, the following code creates an array callednumbers and iterates through it with the foreach statement:

    With multidimensional arrays, you can use the same method to iterate through the elements, for example:

    The output of this example is:

    However, with multidimensional arrays, using a nested for loop gives you more control over the array elements.

    See AlsoC# Tutorials

    2015 Microsoft

    numbers[1][1]=667;

    int[]numbers={1,2,3,4,5};intLengthOfNumbers=numbers.Length;

    int[]numbers={4,5,6,1,2,3,2,1,0};foreach(intiinnumbers){System.Console.WriteLine(i);}

    int[,]numbers=newint[3,2]{{9,99},{3,33},{5,55}};foreach(intiinnumbers){Console.Write("{0}",i);}

    999333555