working with arrays
Answer / kris
Initializing Arrays
C# 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 initialize different kinds of arrays.
Note If 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
you declare the array as a field of a type, it will be set
to the default value null when you instantiate the type.
Single-Dimensional Array
int[] numbers = new int[5] {1, 2, 3, 4, 5};
string[] names = new string[3] {"Matt", "Joanne", "Robert"};
You can omit the size of the array, like this:
int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Matt", "Joanne", "Robert"};
You can also omit the new operator if an initializer is
provided, like this:
int[] numbers = {1, 2, 3, 4, 5};
string[] names = {"Matt", "Joanne", "Robert"};
Multidimensional Array
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[2, 2] { {"Mike","Amy"},
{"Mary","Albert"} };
You can omit the size of the array, like this:
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[,] { {"Mike","Amy"},
{"Mary","Albert"} };
You can also omit the new operator if an initializer is
provided, like this:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
Jagged Array (Array-of-Arrays)
You can initialize jagged arrays like this example:
int[][] numbers = new int[2][] { new int[] {2,3,4}, new
int[] {5,6,7,8,9} };
You can also omit the size of the first array, like this:
int[][] numbers = new int[][] { new int[] {2,3,4}, new int[]
{5,6,7,8,9} };
-or-
int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
Notice that there is no initialization syntax for the
elements of a jagged array.
Accessing Array Members
Accessing 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 5 to the fifth element of the array:
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
numbers[4] = 5;
The following code declares a multidimensional array and
assigns 5 to the member located at [1, 1]:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
numbers[1, 1] = 5;
The following is a declaration of a single-dimension jagged
array that contains two elements. The first element is an
array of two integers, and the second is an array of three
integers:
int[][] numbers = new int[][] { new int[] {1, 2}, new int[]
{3, 4, 5}
};
The following statements assign 58 to the first element of
the first array and 667 to the second element of the second
array:
numbers[0][0] = 58;
numbers[1][1] = 667;
Is This Answer Correct ? | 3 Yes | 0 No |
Code for Working with Files under a Directory?
Can you declare an array of mixed Types?
Code for Searching for Multiple Matches with the MatchCollection Class?
program for string reverse(eg:- i am boy -> boy am i)
8 Answers Black Pepper, Infosys, Mind Tree,
IS Array list is generic or non generic
Write a program to count 3Letter, 4Letter and 5Letter words from a file and print the number of 3Letter, 4Letter and 5Letter words. Delimiter is space, tab, hifen. Also we should not consider the line in the file after we encounter # in that line.
program to reverse the order of digits in a given number of any length.
c# coding for a program that takes a 3 digit number n and finds out whether the number 2^n + 1 is prime, or if it is not prime find out its factors not using the Big-integer and Exponential Function's.
program for addition of fraction(M/N + P/Q = Y/Z)
How to add a value from textBox over an existing certain column in SQL Server
How to find No of classes,Packages,No of Methods per Classes and Depth of Inheritance for selecting source code in windows form application using c# .net? (Source code is input Program. It may be Java or .net) Please help me..) Thanks..)
How to pass multiple rows from one gridview to another gridview after clicking the checkbox.