LogIn
I don't have account.

List in C#

DevSniper
116 Views

Namespace :- System.Collections.Generic
Assembly :- System.Collections.dll
Signature :-
public class List<T> : 
   System.Collections.Generic.ICollection<T>,
   System.Collections.Generic.IEnumerable<T>,
   System.Collections.Generic.IList<T>,
   System.Collections.Generic.IReadOnlyCollection<T>,
   System.Collections.Generic.IReadOnlyList<T>,
   System.Collections.IList
Constructors
1. public List ();
2. public List (System.Collections.Generic.IEnumerable<T> collection);
3. public List (int capacity);

Properties

1. Capacity
public int Capacity { get; set; }
2. Count
public int Count { get; }
3. Item[Int32]
public T this[int index] { get; set; }
Creating and adding element in a List
1. using collection initializer
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var list = new List<string> {"C#","Java","C++","Python"};
        Console.WriteLine($" index 0 value : {list[0]} and index 2 value : {list[2]}");
    }
}
index 0 value : C# and index 2 value : C++
2. using Add() method
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var list = new List<string>();
        list.Add("C#");
        list.Add("Java");
        list.Add("C++");
        list.Add("Python");
        Console.WriteLine($" index 0 value : {list[0]} and index 2 value : {list[2]}");
    }
}
index 0 value : C# and index 2 value : C++
IMPORTANT POINTS
It can has duplicate elements.
It will store strongly typed of objects.
List element can be accessed by index.