Use of “List< DataType >” :

“I have discussed here about ArrayList but there is a disadvantage of using ArrayList.We can’t customize data type in ArrayList that means we can add any kind of data type in it which is not a good process for OOP.
So instead of using ArrayList we can use List<DataType> in which we can define data type.”

class Program
{
public static void Main()
{
List<int> list = new List<int>();
list.Add(2);
list.Add(3);               // Add two integers in List<int>

Example(list);           // Here Example is a method
}

static void Example(List<int> list)
{
foreach(int abc in list)
{
Console.Writeline(abc);
Console.ReadLine();
}
}
}
OUTPUT :
2
3

How to reverse a string :

class Program

{
static void Main(string[] args)
{
string reverseName = ” “;
Console.WriteLine(“Please give an input :”);
string fullName = Console.ReadLine();   //Take user input.

for (int index = fullName.Length – 1; index >= 0; index–)
{
reverseName = reverseName + fullName[index];
}

    Console.WriteLine(reverseName);
Console.ReadLine();
}
}
Input :       I am a student.
Output :    .tneduts a ma I

Use of “while loop” :

class Program
{
  static void Main(string[] args)
  {
    int total=0;
    int gradeCounter=0;
    int gradeValue;
    double average;
 
    Console.Write(“Enter -1 to quit :”);
    gradeValue = Int32.Parse(Console.ReadLine());  // Take user input in gradeValue.
 
    while (gradeValue!=-1)         //When gradeValue == -1 then while loop will close.
    {
      total = total + gradeValue;
      gradeCounter = gradeCounter + 1;     //To know the student count Add 1.
      Console.Write(“Enter -1 to quit :”);
      gradeValue = Int32.Parse(Console.ReadLine());
    }
            
    if(gradeCounter!=0)
    {
      average = (double) total/gradeCounter;
      Console.WriteLine(“nAverage number : {0} nTotal count:{1}” ,average,gradeCounter);
      Console.ReadLine();
    }
     
    else
    {
      Console.WriteLine(“nNo grade were entered.”);
      Console.ReadLine();
     }
  } 
}

How to use GetRange() :

class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(“One”);
list.Add(“Six”);
list.Add(“Four”);

ArrayList range = list.GetRange(0,3);
foreach (string s in range)
{
Console.WriteLine(s);
Console.ReadLine();
}
}
}
Output : 
One
Six
Four

N.B : Let            list.GetRange(m,n);
Here ,        m = position of starting element of an array
n = how many number of element(s) want to show serially from m


Other outputs :

list.GetRange(0,1)
list.GetRange(0,2)
list.GetRange(0,3)
One
One
One
Six
Six
Four

.

list.GetRange(1,1)
list.GetRange(1,2)
Six
Six
Four

 

list.GetRange(2,1)
Four

Use of “for loop” to show the elements of an ArrayList :

class Program
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add(“Imrez”);
list.Add(“Khan”);
list.Add(“Ratin”);

for(int i=0; i<list.Count;i++)
{
string value = list[i].ToString();
Console.WriteLine(value);
Console.ReadLine();
}

}
}

Output :
Imrez
Khan
Ratin