Working with array in C#

Submitted by: 
Language: 

Objective

Like other programming language that supports array, C# gives you an efficient and simple way to manipulate with array. This tutorial will show you about array in C#, what it is and how array works.

Let's go

Imagine that you have to arrange array of numbers in incremental direction, or you need to manipulate with 1000 objects of specific type, so what is efficient way you catch all of these and start doing with?
Absolutely, the easy way is fetch these objects/values into an array. You can have many elements of array – but just remember that choose the best to make the program efficiently.
To works with array, you need to declare an array of type that your data belong to. For example, we declare an array of Int type:

  1. int[] my_arr = new int[100];

In line of code above, we simple declare 100 elements in array my_arr, each element has type of Int. When you declare an array, each element in array will be assigned a default value, in above declaration, each element has a default value 0. To assign a value to a specific element, just specify the element with index like example bellow:

  1. my_arr[5] = 1; //assigns value 1 to element 5

You can also initialize the values of elements in array by assigning when declaring the array. For example:

  1. int[] my_arr = {1,2,3,4,5};

You should note that the array start with index 0 and the maximum value of index in example above is 99. That easy to declare an array, but in case of you don’t know exactly number of elements in an array, so how can you declare an array? In that case you don’t need to specific number of elements in the array. Example bellow demonstrates how you can do that:

  1. static void Main(string[] args)
  2. {
  3. int[] arr;
  4. //declare an random object
  5. Random rd = new Random();
  6. //get random number
  7. int n = rd.Next(100);
  8. //specify number of elements in array
  9. arr = new int[n];
  10. Console.WriteLine("array has length: " + n);
  11. Console.ReadKey();
  12. }

In above code we just declare an array, but not specify the number of elements until we get the random value. Look, everything is simple in C#.
Multiple dimensional array

You can declare a multiple dimensional array. Let see an example bellows:

String[,] str_arr = new String[2, 3];
In above code, we declare a multiple dimensional array, with two rows and three columns. We also declare and assign value to array by doing:
String[,] str_arr = {{"a","b"},{"c","d"},{"3","f"}};
When you want to get value from multiple dimensional array, just give index of each dimension. For example, if we want to get value “a” from above array, just use: str_arr[0,0];

Nested array

An array can be nested by other array. Example of nested array as follow:

  1. //declare a two level nested array
  2. int[][] arr = new int[100][];
  3. //declare the first child array in the nested array
  4. arr[0] = new int[10];
  5. //declare the second child array in the nested array
  6. arr[1]=new int[20];

In above code:

  • We declare a parent array with two level nested array named arr
  • We declare the first child array of the nested array that has 10 elements
  • We declare the second child array that has 20 elements

You should note that the number of elements in the child array is independent of the number of elements of parent array.

Summary

In this tutorial you have been shown about the array and how to use array in C# program. Array makes C# more powerful. With array you can do many things like containing groups object for arranging, finding/searching, computing…So far, try to test with array and discover the power of it.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.