Learn how to work with Arrays using C#

In this article, we will take a close look on how to work with Arrays. We will also examine various Access Modifiers and how to work with command line arguments in C#.

Arrays facilitate us to store a list of items of similar data type. For example, to store the names of students you can use an array of type string and to store their ages, integer type can be used.

In C#, arrays are derived from System.Array class. There are lot of properties and methods in this class, which we can manipulate for our programming tasks. Arrays in C# are declared in the same fashion as in Java, C++ and it can be either single or multi dimensional. The following code declares a single dimensional array of type integer and with dimension of 10. Dimension indicates number of array elements.

Listing 1

int[] x = new int[10]

Arrays can also be declared as in the following code:

Listing 2

int[] x = {2,3,4,5,6}

The above statement stores 4 integer values, as the index position for the first element will be always zero. Listing 3 below illustrates how to print the values in an array.

Listing 3

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System; 
class PrintArray 
{ 
public static void Main(String[] args) 
{ 
int[] x = {10,20,30,40,50}; 
for(int i = 0; i<span style="color: #0000ff">&lt;</span>=4;i++) 
Console.WriteLine(x[i]); 
}
}
}

You can also use the Length property of System.Array class to determine the dimensions of an array. Listing 4 makes use of this useful property.

Listing 4

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System; 
class Printlength 
{ 
public static void Main(String[] args) 
{ 
int[] x = {10,20,30,40,50}; 
for(int i = 0; i<span style="color: #0000ff">&lt;</span><span style="color: #800000">x.Length</span>;<span style="color: #ff0000">i</span>++) 
{ 
<span style="color: #ff0000">Console</span>.<span style="color: #ff0000">WriteLine</span>(<span style="color: #ff0000">x</span>[<span style="color: #ff0000">i</span>]); 
}
}
}

C# also supports Multidimensional arrays. It can be represented as shown in Listing 5:

READ  Understanding Data Types and Variables in C#

Listing 5

// Three dimensional array (Count number of 
commas and add 1 to it)
int[,,] x 
// Two dimensional array
int[,] y
&nbsp;

The Listing given below explains the usage of multidimensional arrays:

Listing 6

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System; 

class Multi 

{ 

public static void Main() 

{ 

int[,]x; 

x = new int[4,4]; 

x[0,0] = 1; 

x[1,1] = 1; 

x[2,2] = 1; 

x[3,3] = 1; 

Console.WriteLine(x[0,0] +" " +x[0,1] + " " +x[0,2] + " "+x[0,3]); 

Console.WriteLine(x[1,0] +" " +x[1,1] + " " +x[1,2] + " "+x[1,3]); 

Console.WriteLine(x[2,0] +" " +x[2,1] + " " +x[2,2] + " "+x[2,3]); 

Console.WriteLine(x[3,0] +" " +x[3,1] + " " +x[3,2] + " "+x[3,3]); 

} 

}

Multidimensional arrays are also called as rectangular arrays, in which each row will be of same length. C# also supports yet another type of array called Jagged Array. It’s termed as an array of arrays. In case of Jagged Array each row will be of different length. It can be represented as shown below:

Listing 7

// Jagged Array
int [][] x = new int[2][];

The Listing given below illustrates the usage of Jagged Array:

Listing 8

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System; 

class Jagged 

{ 

int [][] x = new int[2][]; 

void set() 

{ 

x[0] = new int[2]; 

x[1] = new int[2]; 

x[0][0] = 1; 

x[0][1] = 2; 

} 

void show() 

{ 

for(int i = 0; i<span style="color: #0000ff">&lt;</span>2;i++){ 

for(int j = 0;j<span style="color: #0000ff">&lt;</span>2;j++) { 

Console.WriteLine(x[i][j]); 

} 

} 

} 

public static void Main() { 

Jagged j = new Jagged(); 

j.set(); 

j.show(); 

} 

}

You can programmatically determine the dimension of an array by using Array.Rank property as shown in Listing 9:

Listing 9

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System; 

class Ranks 

{ 

int[] SD; 

int[,] MD; 

int[,,] TD; 

Ranks() 

{ 

SD = new int[10]; 

MD = new int[10,20]; 

TD = new int[10,20,30]; 

} 

void Display() 

{ 

Console.WriteLine("SD = {0}",SD.Rank); 

Console.WriteLine("MD = {0}",MD.Rank); 

Console.WriteLine("TD = {0}",TD.Rank); 

} 

public static void Main(string[] args) 

{ 

Ranks r = new Ranks(); 

r.Display(); 

} 

}

Access Modifiers

Access Modifiers determine the scope of visibility for variables and methods. There are four types of modifiers in C# viz. Public, Protected, Private and Internal. For example, a variable declared as private in a super class cannot be called in a sub class. It should be declared as either public or protected. Let’s examine each of these in detail.

READ  Undertanding Arrays & Inheritance using Java

1. Public members are accessible from outside the class definition.

2. Protected members are not visible outside the class and can be accessed by derived or sub classes only.

3. Private member’s scope is limited to the defined class. Derived classes or any other classes cannot access these members.

4. Internal members are visible only within the current compilation unit.

Working with command line arguments

Normally, we use to pass values and input information by using the keyboard. This functionality can be easily achieved using C++ by making use of cin keyword. But, there is no such keyword in C#. You have to call some advanced namespaces and classes, which we will examine later. However, this functionality is achievable by passing command line arguments. The listing 10 given below examines this concept:

Listing 10

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">using System; 
class CommandLine 
{ 
public static void Main(string[] args) 
{ 
foreach(string arg in args) 
{ 
Console.WriteLine(“Argument :{0}”,arg); 
}
}
}

Leave a Reply

Your email address will not be published.