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.
๐ What You Are Going to Learn in This Lesson?
โ๏ธ What is Generics List<T> in C#?
โ๏ธ Why is it better than arrays?
โ๏ธ Basic syntax and how it works.
โ๏ธ Important methods (Add, Remove, Sort, etc.).
โ๏ธ A real-world example for better understanding.
โ๏ธ A complete program with detailed explanation.
So, letโs dive in! ๐
โ 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 (likeint
,string
,Person
, etc.).listName
โ The name of the List.new List<T>()
โ Creates an empty list of typeT
.
ย
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! ๐
Method | Description | Example |
---|---|---|
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); |
Count | Returns 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! ๐ฅ