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 Searching for Multiple Matches with the MatchCollection Class?
Code for Reading and writing from a file?
Event Handling in C# Triggering a Button
program for string reverse(eg:- i am boy -> boy am i)
8 Answers Black Pepper, Infosys, Mind Tree,
program to reverse the order of words in a string.
IS Array list is generic or non generic
Code for Reading and writing from a file in c#?
program for addition of fraction(M/N + P/Q = Y/Z)
Hello I am hosting remoting within IIS. Everything works just fine as long as I allow anonymous access at the IIS level. When I allow only Windows Authenticated sessions, something very strange happens: If the client activates the remote object with IP address - works fine If the client activates it using a machine name - get http 401 exception is thrown. The exception is thrown when the client calls the remote method. I set the channel's useDefaultCredentials property to true. Any idea how to allow Windows Authentication? Here is the call stack: System.Net.WebException: The remote server returned an error: (401) Unauthorized .. ---> System.ComponentModel.Win32Exception: The target principal name is incorrect at System.Net.NTAuthentication.GetOutgoingBlob(Byte[] incomingBlob, Boolean handshakeComplete) at System.Net.NTAuthentication.GetOutgoingBlob(String incomingBlob, Boolean handshakeComplete) at System.Net.NegotiateClient.DoAuthenticate(String challenge, WebRequest webRequest, ICredentials credentials, Boolean preAuthenticate) at System.Net.NegotiateClient.DoAuthenticate(String challenge, WebRequest webRequest, ICredentials credentials, Boolean preAuthenticate) at System.Net.NegotiateClient.Authenticate(String challenge, WebRequest webRequest, ICredentials credentials) at System.Net.AuthenticationManager.Authenticate(String challenge, WebRequest request, ICredentials credentials) at System.Net.AuthenticationState.AttemptAuthenticate (HttpWebRequest httpWebRequest, ICredentials authInfo) --- End of inner exception stack trace ---
. Write a program to print the following outputs using for loops $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
Coding for using Nullable Types in C#?
Write a program. there are 1..n numbers placed in an array in random fashion with one integer missing. find the missing number.