C# List and Uses

List<T> class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List<T> class also provides the methods to search, sort, and manipulate lists.

Example: List<T>


List<int> intList = new List<int>();

//Or

IList<int> intList = new List<int>();
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
[System.Serializable]
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                                                    
How to Add Elements into Lists
using System;
using System.Collections.Generic;

public class Program
{
 public static void Main()
 {
  IList<int> intList = new List<int>();
  intList.Add(10);
  intList.Add(20);
  intList.Add(30);
  intList.Add(40);
  
  Console.WriteLine(intList.Count);
  
  IList<string> strList = new List<string>();
  strList.Add("one");
  strList.Add("two");
  strList.Add("three");
  strList.Add("four");
  strList.Add("four");
  strList.Add(null);
  strList.Add(null);
  
  Console.WriteLine(strList.Count);
  
  IList<Student> studentList = new List<Student>();
  studentList.Add(new Student());
  studentList.Add(new Student());
  studentList.Add(new Student());
  
  Console.WriteLine(studentList.Count);
  
 }
}

public class Student
{ 
 
}


Rahul Singh

As a passionate software developer, trainer, and tech blogger, I thrive on sharing knowledge and exploring the latest in technology. With a strong foundation in programming languages like C and C++, and expertise in platforms like Microsoft 365, SharePoint, and Azure, I aim to simplify complex concepts for others. My blog, Expert2Code.com, is a space where I document my learning journey, providing insights and resources for those eager to master the tech world. When I'm not coding or teaching, you'll find me reading, hiking, or discovering new tools and techniques to stay ahead in this fast-paced industry.

Post a Comment

Previous Post Next Post