Learn how to work with Arrays using C#


0

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

using System; 
class PrintArray 
{ 
public static void Main(String[] args) 
{ 
int[] x = {10,20,30,40,50}; 
for(int i = 0; i<=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

using System; 
class Printlength 
{ 
public static void Main(String[] args) 
{ 
int[] x = {10,20,30,40,50}; 
for(int i = 0; i<x.Length;i++) 
{ 
Console.WriteLine(x[i]); 
}
}
}

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

Listing 5

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

The Listing given below explains the usage of multidimensional arrays:

Listing 6

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

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<2;i++){ 

for(int j = 0;j<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

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.

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

using System; 
class CommandLine 
{ 
public static void Main(string[] args) 
{ 
foreach(string arg in args) 
{ 
Console.WriteLine(“Argument :{0}”,arg); 
}
}
}

Like it? Share with your friends!

0

What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win
Anand Narayanaswamy
Anand Narayanaswamy is the editor-in-chief of Learnxpress. He was a Microsoft Most Valuable Professional (MVP) for a period of 9 years. He is a ASPInsider based in Trivandrum, Kerala State, India. Anand is the author of Community Server Quickly published by Packt Publishing.

0 Comments

Your email address will not be published. Required fields are marked *