Complete C# Tutorial

The Ultimate Guide to Generics List <T> in C# (With Examples!)

Introduction

โ“ Are you tired of using arrays and struggling with their fixed sizes? ๐Ÿ˜Ÿ
โ“ Do you wish for a more flexible, dynamic way to store data? ๐Ÿค”

Well, Generics List<T> in C# is here to save the day! ๐ŸŽ‰ Itโ€™s like an array on steroidsโ€”resizable, type-safe, and packed with cool features. In this guide, youโ€™ll master List<T>, see real-world examples, and write code like a pro! ๐Ÿ˜Ž

But first, letโ€™s talk about why you need it.

โ“ Why is Generics List<T> in C# Important?

  • Unlike arrays, List<T> grows automatically when needed.
  • Itโ€™s type-safe, meaning no boxing/unboxing issues.
  • Provides built-in methods for adding, removing, searching, and sorting.
  • Used in real-world applications like e-commerce carts, student records, and task lists.

๐Ÿ› ๏ธ Syntax of Generics List<T> in C#

Before jumping into examples, letโ€™s understand the basic syntax.

				
					List<T> listName = new List<T>();
				
			

๐Ÿ“Œ Explanation:

  • T โ†’ Generic type parameter (like int, string, Person, etc.).

  • listName โ†’ The name of the List.

  • new List<T>() โ†’ Creates an empty list of type T.

ย 

Example:

Letโ€™s create a List of integers and add some numbers!

				
					using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>(); // Create a list of integers

        numbers.Add(10); // Add elements
        numbers.Add(20);
        numbers.Add(30);

        Console.WriteLine("List contains: " + string.Join(", ", numbers));
    }
}
				
			

๐Ÿ–ฅ๏ธ Output:

				
					List contains: 10, 20, 30
				
			

๐ŸŽฏ Boom! Your first List<T> is ready!

๐Ÿ’ก Key Methods of Generics List<T> in C#

Here are some must-know methods youโ€™ll use daily! ๐Ÿš€

MethodDescriptionExample
Add(T item)Adds an item to the list.numbers.Add(40);
Remove(T item)Removes the first occurrence of an item.numbers.Remove(20);
CountReturns the number of items in the list.numbers.Count;
Contains(T item)Checks if the item exists in the list.numbers.Contains(30);
Sort()Sorts the list in ascending order.numbers.Sort();
Reverse()Reverses the list order.numbers.Reverse();

๐Ÿ“Œ Example Code Using These Methods

				
					using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };

        numbers.Add(60);  // Add an item
        numbers.Remove(20);  // Remove an item
        numbers.Insert(2, 25); // Insert at index 2
        numbers.Sort(); // Sort the list
        numbers.Reverse(); // Reverse the list

        Console.WriteLine("List Elements: " + string.Join(", ", numbers));

        Console.WriteLine("List Contains 30? " + numbers.Contains(30));
        Console.WriteLine("Index of 25: " + numbers.IndexOf(25));

        List<int> greaterThan30 = numbers.FindAll(x => x > 30);
        Console.WriteLine("Numbers greater than 30: " + string.Join(", ", greaterThan30));
    }
}
				
			

๐Ÿ–ฅ๏ธ Output:

				
					List Elements: 60, 50, 40, 30, 25, 10
List Contains 30? True
Index of 25: 4
Numbers greater than 30: 60, 50, 40
				
			

๐Ÿ“ Example 1: Working with List<T> Methods

Letโ€™s see these methods in action! ๐ŸŽฏ

				
					using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> names = new List<string> { "Alice", "Bob", "Charlie" };

        names.Add("David"); // Add a name
        names.Remove("Bob"); // Remove a name
        names.Sort(); // Sort names

        Console.WriteLine("Names: " + string.Join(", ", names));
    }
}
				
			

๐Ÿ–ฅ๏ธ Output:

				
					Names: Alice, Charlie, David
				
			

๐ŸŒŽ Real-World Example: Shopping Cart System ๐Ÿ›’

Letโ€™s say youโ€™re building a shopping cart system for an e-commerce website. How do you store items dynamically? Using Generics List<T> in C#!

๐Ÿ’ป Code Example:

				
					using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> cart = new List<string>(); // Shopping cart

        cart.Add("Laptop");
        cart.Add("Mouse");
        cart.Add("Keyboard");

        Console.WriteLine("๐Ÿ›’ Shopping Cart: " + string.Join(", ", cart));

        cart.Remove("Mouse"); // Removing an item

        Console.WriteLine("๐Ÿ›’ After Removing Mouse: " + string.Join(", ", cart));
    }
}
				
			

๐Ÿ–ฅ๏ธ Output:

				
					๐Ÿ›’ Shopping Cart: Laptop, Mouse, Keyboard
๐Ÿ›’ After Removing Mouse: Laptop, Keyboard
				
			

๐Ÿ›๏ธ This is how real-world shopping carts store and manage products! Cool, right? ๐Ÿ˜Ž

๐Ÿ”„ Example 2: Storing Custom Objects in List<T>

You can also store custom objects inside List<T>!

				
					using System;
using System.Collections.Generic;

class Product
{
    public string Name { get; set; }
    public double Price { get; set; }
}

class Program
{
    static void Main()
    {
        List<Product> products = new List<Product>
        {
            new Product { Name = "Phone", Price = 799.99 },
            new Product { Name = "Tablet", Price = 499.99 }
        };

        foreach (var p in products)
        {
            Console.WriteLine($"{p.Name} - ${p.Price}");
        }
    }
}
				
			

๐Ÿ–ฅ๏ธ Output:

				
					Phone - $799.99
Tablet - $499.99
				
			

๐ŸŽฏ Conclusion

So, what did we learn today? ๐Ÿค”

  • โžก๏ธ List<T> is a dynamic, resizable, type-safe alternative to arrays.
  • โžก๏ธ It comes with powerful built-in methods like Add, Remove, Sort, etc.
  • โžก๏ธ Itโ€™s widely used in real-world applications like shopping carts and databases.

Isn’t Generics List<T> in C# amazing? Now, go ahead and experiment with it! ๐Ÿ’ช

ย 

โญ๏ธ Next What?

Ready to take your learning to the next level? ๐Ÿš€
In the next chapter, youโ€™ll learn about Generics LinkedList<T> in C#, another awesome collection type that works differently from List<T>. Stay tuned! ๐Ÿ˜Š

๐Ÿ”ฅ Keep coding and keep growing! ๐Ÿ”ฅ

Leave a Comment

Share this Doc

Generics List<T>

Or copy link