working with arrays



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

Post New Answer

More C Sharp Code Interview Questions

how to change password in .net with c # with ado.net and also SQL server 2008 change password

1 Answers  


how to get the table names via c sharp and column names also?

2 Answers   Sify,


Create a class called Accounts which has data members like ACCOUNT no, Customer name, Account type, Transaction type (d/w), amount, balance D->Deposit W->Withdrawal If transaction type is deposit call the credit(int amount) and update balance in this method. If transaction type is withdraw call debit(int amt) and update balance. Pass the other information like Account no,name,Account Type through constructor. Call the show data method to display the values.

1 Answers   Cognizant,


program for addition of fraction(M/N + P/Q = Y/Z)

1 Answers   Mind Tree,


Code for IP Address Comparison and Conversion in C#?

0 Answers  






Coding for using Nullable Types in C#?

1 Answers  


How to pass multiple rows from one gridview to another gridview after clicking the checkbox.

1 Answers   Satyam,


how do i copy textbox contents of 1 form to another form

4 Answers   Wipro,


Using C# Write a program that performs the following. The user inputs a number and then enters a series of numbers from 1 to that number. Your program should determine which number (or numbers) is missing or duplicated in the series, if any. For example, if the user entered 5 as the initial number and then entered the following sequences, the results should be as shown. Input Sequence Output ---------------------- --------------- 1 2 3 4 5 Nothing bad However, if 7 were the high number, the user would see the results on the right for the following number entries: Input Sequence Output ---------------------- --------------- 1 3 2 4 5 Missing 6 Missing 7 And if 10 were the high number and the user entered the numbers shown on the left, note the list of missing and duplicate numbers: Input Sequence Output ---------------------- --------------- 1 2 4 7 4 4 5 10 8 2 6 Duplicate 2 ( 2 times) Missing 3 Duplicate 4 ( 3 times ) Missing 9 The program should check the high number that the user inputs to ensure that it does not exceed the size of any array you might be using for storage.

0 Answers  


Give the code for Handling Mouse Events?

0 Answers  


program for string reverse(eg:- i am boy -> boy am i)

8 Answers   Black Pepper, Infosys, Mind Tree,


I am developing a web application using google map api.I want to update the map inside the div control within update panel. I got other controls updated but map doesn't get updated.I do not not want to update map whenever unnecessary controls are fired at server side.How could it be achieved?

0 Answers  


Categories
  • ASP.NET Code Interview Questions ASP.NET Code (46)
  • VB.NET Code Interview Questions VB.NET Code (9)
  • C Sharp Code Interview Questions C Sharp Code (51)
  • ADO.NET Code Interview Questions ADO.NET Code (8)