Array in C# how to use and example

Array in C# how to use and example:





As we know that array is the collection of similar types of data.

Declaring Arrays:

To declare an array in C#, you can use the following syntax:

datatype[] arrayName;

where,

  • datatype is used to specify the type of elements to be stored in the array.
  •  
  • [ ] specifies the rank of the array. The rank specifies the size of the array.
  •  
  • arrayName specifies the name of the array.


Array example C#:


double[] balance;


public static string[] stringArray = new string[5] { "A", "C", "B", "E", "D" };

 public static Boolean flag = true;

 static void Main(string[] args)
 {
 while (flag)
 {
 Console.WriteLine("\n1. Lenght \t\t2. Sort \n3. Display\t\t4. Exit");

 Console.Write("Please enter your Choice: ");

 string choice = Console.ReadLine();

 switch (choice)
 {
 case "1":

 Length(stringArray);

 break;

 case "2":

 Sort(stringArray);

 break;

 case "3":

 Display(stringArray);

 break;

 case "4":

 flag = false;

 break;

 default:

 Console.WriteLine("Enter correct choice !!");

 break;

 }
 }
 }
private static void Length(string[] stringArray)

 {

 int count = stringArray.Length;

 Console.WriteLine("Count of Array : " + count);

 }
private static void Sort(string[] stringArray)

 {

 Array.Sort(stringArray);

 Display(stringArray);

 }

private static void Display(string[] stringArray)

 {

 Console.WriteLine("Items in Array : ");

 for (int i = 0; i < stringArray.Length; i++)

 {

 Console.WriteLine("Index " + i + "\t: " + stringArray[i]);

 }
 }

Arrays in asp.net C#


Asp.net Related Other Post:

Comments

Popular posts from this blog