Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C#
  4. Complete Guide to Lists in C#: Examples and Best Practices

Complete Guide to Lists in C#: Examples and Best Practices

Date- Dec 09,2023 Updated Feb 2026 3231
csharp list class

C# List class is used to store and fetch elements. It can also have duplicate elements. It is found in System.Collections.Generic namespace. List class in C# represents a strongly typed list of objects. List provides functionality to create a list of objects, find list items, sort list, search list, and manipulate list items. In List, T is the type of object. What is the ‘T’ in List? List class represents a strongly typed list of objects. List provides functionality to create a list of objects, find list items, sort list, search list, and manipulate list items. In List, T is the type of objects. How to create List in C#? List is a generic class and is defined in the System.Collections.Generic namespace. You must import this namespace in your project to access the List class. using System.Collections.Generic; The List is a generic collection, so you need to specify a type parameter for the type of data it can store

The following example shows how to create a integer type list and add elements in list.


using System;
using System.Collections.Generic;


public class Program
{
	public static void Main()
	{
		// adding elements using add() method
		List Numbers = new List();
		Numbers.Add(1);
		Numbers.Add(2);
		Numbers.Add(3);
		Numbers.Add(4);
		
			   foreach(var i in Numbers)
			   {
				  Console.WriteLine("First number : " +  i);
			   }
	}
}
 

Output


First number : 1
First number : 2
First number : 3
First number : 4
 

The following example shows how to create a string type list and add elements in list.


using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		// adding elements using add() method
			var cities = new List();
			cities.Add("Yamuna Nagar");
			cities.Add("Kurukshetra");
			cities.Add("Panipat");
			cities.Add("Ambala");
		
			   foreach(var i in cities)
			   {
				  Console.WriteLine("City Name: " +  i);
			   }
	}
} 

Output


City Name: Yamuna Nagar
City Name: Kurukshetra
City Name: Panipat
City Name: Ambala
 

The following example shows how to adding elements using collection initializer syntax


using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		// adding elements using collection initializer syntax
		var cities = new List() {"Kurukshetra", "Panipat", "Ambala", "Yamuna Nagar"};
			   foreach(var i in cities)
			   {
				  Console.WriteLine("City Name: " +  i);
			   }
	}

} 

Output


City Name: Kurukshetra
City Name: Panipat
City Name: Ambala
City Name: Yamuna Nagar
 

The following example shows how to adding elements into list using classe


using System;
using System.Collections.Generic;
public class Program
{
	public static void Main()
	{
		var students = new List() { 
                new Student(){ Id = 1, Name="Shubham"},
                new Student(){ Id = 2, Name="mohit"},
                new Student(){ Id = 3, Name="Rohan"},
                new Student(){ Id = 4, Name="Rahul"}
            };
	
			foreach(var name in students)
			{
				Console.WriteLine("Student Name: " + name.Name);
			}
		
	}
}

class Student{
	public int Id { get; set; }
	public string Name { get; set; }
}
 

Output


Student Name: Shubham
Student Name: mohit
Student Name: Rohan
Student Name: Rahul
 

In the above example, List Numbers = new List(); creates a list of int type. In the same way, cities are string type list. You can then add elements in a list using the Add() method or the collection-initializer syntax. You can also add elements of the custom classes using the collection-initializer syntax. The following adds objects of the Student class in the List.

Example: Add Custom Class Objects in List


                var students = new List() { 
                new Student(){ Id = 1, Name="Shubham"},
                new Student(){ Id = 2, Name="mohit"},
                new Student(){ Id = 3, Name="Rohan"},
                new Student(){ Id = 4, Name="Rahul"}
 
collection)

Example: Add Arrays in List


using System;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		string[] cities = new string[3]{ "Kurukshetra", "Panipat", "Ambala" };

			var popularCities = new List();

			// adding an array in a List
			popularCities.AddRange(cities);

			var favouriteCities = new List();

			// adding a List 
			favouriteCities.AddRange(popularCities);
		
			foreach(var i in favouriteCities)
			  {
				  Console.WriteLine("City Name: " +  i);
			  }
	}

}	   
 

Output


City Name: Kurukshetra
City Name: Panipat
City Name: Ambala
 

Accessing a List using LINQ The List implements the IEnumerable interface. So, we can query a list using LINQ query syntax or method syntax, as shown below.

Example: LINQ Query on List


using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
	public static void Main()
	{
		var students = new List() { 
                new Student(){ Id = 1, Name="shubham" },
                new Student(){ Id = 2, Name="rahul" },
                new Student(){ Id = 3, Name="abir" },
                new Student(){ Id = 4, Name="shubham" },
				new Student(){ Id = 5, Name="mohit" }
		};
		
		//get all students whose name is shubham
		var Student = from s in students
			where s.Name == "shubham"
			select s;
		
		foreach(var student in Student)
			Console.WriteLine("student Id " + student.Id + " , student Name " +student.Name);
	}
}

public class Student
{ 
	public int Id { get; set; }
	public string Name { get; set; }
}
 

Output


City Name: Kurukshetra
City Name: Panipat
City Name: Ambala
 

Insert Elements in List Use the Insert() method inserts an element into the List collection at the specified index. Insert() signature:void Insert(int index, T item);

Example: Insert elements into List


using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		List numbers = new List(){ 1,  3, 4 };

		numbers.Insert(1, 2);// inserts 11 at 1st index: after 10.
		
		foreach (var num in numbers)
			Console.WriteLine(num);
	}
}
 

Output


1
2
3
4
 

Remove Elements from List Use the Remove() method to remove the first occurrence of the specified element in the List collection. Use the RemoveAt() method to remove an element from the specified index. If no element at the specified index, then the ArgumentOutOfRangeException will be thrown. Remove() signature: bool Remove(T item) RemoveAt() signature: void RemoveAt(int index)

Example: Remove elements from List


using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		var numbers = new List(){ 1, 2, 3, 4, 5 };

		numbers.Remove(5); // removes 5 elements from a list
		
		numbers.RemoveAt(0); //removes the 3rd element (index starts from 0)
		
		foreach (var num in numbers)
			Console.WriteLine(num);
	}
}
 

Output


2
3
4
 

Check Elements in List Use the Contains() method to determine whether an element is in the List or not.

Example: Contains()


using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		var Name = new List(){ "shubham", "batra", "abir", "mohit" };

		Console.WriteLine(Name.Contains("shubham"));
		Console.WriteLine(Name.Contains("batra"));
		Console.WriteLine(Name.Contains("shubham batra"));
		Console.WriteLine(Name.Contains("mohit"));

	}
}
 

Output


True
True
False
True
 

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Understanding Destructors in C#: A Complete Guide with Examples
Dec 09, 2023
Mastering the foreach Loop in C#: A Complete Guide with Examples
Dec 09, 2023
Configuring NHibernate with ASP.NET Core: A Comprehensive Step-by-Step Guide
Apr 05, 2026
Introduction to C# Programming: Your First Steps in Software Development
Mar 08, 2026
Previous in C#
If Else Statement
Next in C#
Complete Guide to Access Modifiers in C# with Examples
Buy me a pizza

Comments

On this page

🎯

Interview Prep

Ace your C# interview with curated Q&As for all levels.

View C# Interview Q&As

More in C#

  • Zoom C# Wrapper Integration 12905 views
  • Convert HTML String To Image In C# 11504 views
  • The report definition is not valid or is not supported by th… 10856 views
  • Replacing Accent Characters with Alphabet Characters in CSha… 9843 views
  • Get IP address using c# 8688 views
View all C# posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor