You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
C# provides a rich set of collection types and LINQ (Language Integrated Query) for querying and transforming data. Together, they form one of the most powerful features of the language.
Fixed-size, strongly typed collections:
int[] numbers = { 1, 2, 3, 4, 5 };
string[] names = new string[3];
names[0] = "Alice";
// Multi-dimensional
int[,] matrix = { { 1, 2 }, { 3, 4 } };
// Jagged (array of arrays)
int[][] jagged = new int[3][];
jagged[0] = new[] { 1, 2, 3 };
A resizable, generic collection (the most commonly used):
var fruits = new List<string> { "Apple", "Banana", "Cherry" };
fruits.Add("Date");
fruits.Remove("Banana");
fruits.Insert(1, "Blueberry");
Console.WriteLine(fruits.Count); // 3
Console.WriteLine(fruits.Contains("Apple")); // true
Key-value pairs with fast lookup:
var capitals = new Dictionary<string, string>
{
["UK"] = "London",
["France"] = "Paris",
["Germany"] = "Berlin"
};
// Safe lookup
if (capitals.TryGetValue("UK", out string? capital))
{
Console.WriteLine(capital); // London
}
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.