Saturday 16 January 2016

Declare and Use an array in C# with for loop example

2 data types of data C#
a. Value type : Which contains direct value. Ex: String. Int, Float, Double etc
b. Referance type: Which hold the refrence of data. Ex: Array, List, Struct etc.

Array  = Is a collection of same type data type.
Example: Name of students.
string name= "raj";
string[] names = new string[3];
names[0] = "raj";
names[1] = "shyam";
names[2] = "mohan";


int [] ages = new int[3];
age[0] = 12;
age[1] = 32;
age[2] = 42;


using System;
class Program
{
    static void Main()
    {
        string[] names = new string[4];
        Console.WriteLine("Enter name of 3 boys.");
        names[0] = "Raj";
        names[1] = Console.ReadLine();
        names[2] = Console.ReadLine();
        names[3] = Console.ReadLine();

        Console.WriteLine("--------");
        Console.WriteLine(names.Length);
        Console.WriteLine("--------");
        Console.WriteLine("You have entered :- " + names[0] + ", " + names[1] + ", " + names[2] + ", " + names[3]  );



        Console.WriteLine("------printing by using for loop----");
        for (int a = 0; a < names.Length; a++)
        {
            Console.WriteLine(names[a]);
        }


            Console.ReadLine();
    }
}


out put