Arrays in C#

Introduction

I am writing this article to explain each and everything from the base to the extent I am aware about. This article is intended to new developers with basic knowledge of OOPS as well as the professionals working on different technology and need to learn the C# Language.

I was in a difficulty to start this series of article and it was “From Where to start” but after long hours of thinking on the same issue I feel that I am writing for the people who are new in this language and the professionals, so professionals have basic understanding of the Language semantics and the new ones do not know much more, so start from a data type that opens the basic semantics and keywords to the readers.

Arrays in C#

Arrays are just the way to store the similar type of data into sequential form so that it can be accessed by its index and also make it easier to fetch data iteratively. We know the word array from the time of C Language and it works in the same way.

We store the data of same type in an array data structure, an array can be Single Dimensional, Multi-Dimensional and Jagged Array. An array index always starts from 0, and objects in array are set to 0 by default and references by NULL. Arrays types are reference types that are derived from its abstract base type Array and it implements IEnumerable and IEnumerable<T>.

In the above paragraph I have used IEnumerable and Ienumerable<T>, so beginner learners need not to be scared about these words, I will explain them later in this article series.

Note: Array types is reference type but Array are object type that means we can use class members of it, for example,Length, Rank etc.

  1. // Declaration of a single-dimensional array  
  2. int[] myArray = new int[5];  
  3. // Declaration of a Multi-dimensional array  
  4. int[,] myArray1 = new int[5,6];  
  5. // Declaration of a Jagged array  
  6. int[] [] myArray2 = new int[6][];  
  7. // Declaration and set the values of single dimensional array  
  8. int[] myArray3 = { 1, 2, 3, 4, 5, 6 };  
A small array program (Try It).
  1. class TestingClass  
  2. {  
  3.    static void Main()  
  4.    {  
  5.       // Array Declaration and initialization  
  6.       int[,] myArray = new int[5, 10];  
  7.       System.Console.WriteLine("The array has {0} dimensions.", myArray.Rank);  
  8.    }  
  9. }  
Output

 

    The array has 2 dimensions.

Thanks, Stay in Touch.

Ebook Download
View all
Learn
View all