Creating a List in C#

List is used to store values when we do not know about the size it holds in prior.

Source code :

using System;
using System.Collections.Generic;

public class Test
{
	public static void Main()
	{
	
        List<int> val = new List<int>();// creating a list 
        val.Add(1);
        val.Add(3);
        val.Add(5);
        val.Add(7);
        val.Add(9);

        // Printing the values in the list
       
        Console.WriteLine(val[0]);
        Console.WriteLine(val[1]);
        Console.WriteLine(val[2]);
        Console.WriteLine(val[3]);
        Console.WriteLine(val[4]);
        
	}
}


Output :

1
3
5
7
9

Output :


                

Comments :