C# - SPLessons

How to use C# Array

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

How to use C# Array

How to use C# Array

Description :

Hi Friends, let me give you a intro about “Array”. It stores a fixed-size sequential collection of elements of the same type. And its indexes start at zero. Arrays are declared much like variables, with a set of [] brackets after the datatype. 

 

Namespace :

[csharp]

using System;

[/csharp]

 

Step1 :

Please open visual studio, right now i have visual studio 2013 i just opened my visual studio and click on new project.

[image url=””]/wp-content/uploads/2014/09/Array-csharp-Splessons1.jpg[/image]

Step2 :

In the new project window please click on templates visual c# and then click on windows  and on the right side panel just click on console application. Please give a name for the console application and click ok button.

[image url=””]/wp-content/uploads/2014/09/Array-csharp-Splessons2.jpg[/image]

Step3 :

If you see the visual studio will gives you a predefined template program  for us.

[image url=””]/wp-content/uploads/2014/09/Array-csharp-Splessons3.jpg[/image]

Step4 :

Lets, Build simple console application to Find Count, Sort and  Display items from List.

Main Method :

[csharp]

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;
}
}
}
[/csharp]

Output :

[image url=””]/wp-content/uploads/2014/09/Array-csharp-Splessons4.jpg[/image]

Length :

[csharp]

private static void Length(string[] stringArray)
{
int count = stringArray.Length;
Console.WriteLine("Count of Array : " + count);
}
[/csharp]

Output :

[image url=””]/wp-content/uploads/2014/09/Array-csharp-Splessons5.jpg[/image]

Sort :

[csharp]

private static void Sort(string[] stringArray)
{
Array.Sort(stringArray);
Display(stringArray);
}
[/csharp]

Output :

[image url=””]/wp-content/uploads/2014/09/Array-csharp-Splessons6.jpg[/image]

Display :

[csharp]

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]);
}
}
[/csharp]

Output :

[image url=””]/wp-content/uploads/2014/09/Array-csharp-Splessons7.jpg[/image]

 

Enjoy to Code..!!!

 

Comments