C# List

What is List?

In c#, List is a generic type of collection so it will allow storing only strongly typed objects i.e. elements of the same data type, and the size of the list will vary dynamically based on our application requirements like adding or removing elements from the list.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Array
{
    class Program
    {
        static void Main(string[] args)
        {
            List <string> li = new List <string>();  //string list intialize
            for (int i = 1; i <= 4; i++)
            {
                Console.WriteLine("Enter Your List Item "+i.ToString());
                li.Add(Console.ReadLine()+" "); //li.Add() method adding values into list
            }
            foreach (string c in li)
            {
                Console.Write(c); //show the list
            }

            Console.ReadLine();
        }
    }
}

Output

Enter Your List Item 1
I
Enter Your List Item 2
am
Enter Your List Item 3
good
Enter Your List Item 4
boy

I am good boy


Write a comment


Name Email Message