C# — Arrays
Array declaration
using System;
// Array initialization
int[] nums = { 1, 2, 3, 4, 5 };
string[] names = new string[3];
int[] zeros = new int[10];
// Array with size
int[] sized = new int[5]; // All zeros
// Access elements
Console.WriteLine(nums[0]); // 1
Console.WriteLine(nums.Length); // 5
// Modify
nums[0] = 100;
Console.WriteLine(nums[0]); // 100
Array operations
using System;
using System.Linq;
int[] nums = { 5, 2, 8, 1, 9, 3 };
// Sort
Array.Sort(nums);
Console.WriteLine(string.Join(", ", nums)); // 1, 2, 3, 5, 8, 9
// Reverse
Array.Reverse(nums);
Console.WriteLine(string.Join(", ", nums)); // 9, 8, 5, 3, 2, 1
// Find
int idx = Array.IndexOf(nums, 5);
Console.WriteLine($"Index of 5: {idx}");
// Resize
Array.Resize(ref nums, 8);
Console.WriteLine($"New length: {nums.Length}");
// Clear
int[] temp = { 1, 2, 3 };
Array.Clear(temp, 0, 2); // Clear first 2 elements
Multi-dimensional arrays
using System;
// 2D array
int[,] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
Console.WriteLine(matrix[1, 2]); // 6
// Iterate
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"{matrix[i, j]} ");
}
Console.WriteLine();
}
// Jagged array (array of arrays)
int[][] jagged = {
new[] { 1, 2 },
new[] { 3, 4, 5 },
new[] { 6 }
};
foreach (var row in jagged)
{
Console.WriteLine(string.Join(", ", row));
}
List<T>
using System;
using System.Collections.Generic;
var numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Add(6); // Add to end
numbers.Insert(0, 0); // Insert at index
numbers.Remove(3); // Remove first occurrence
numbers.RemoveAt(0); // Remove at index
Console.WriteLine($"Count: {numbers.Count}");
Console.WriteLine($"Contains 4: {numbers.Contains(4)}");
// Iterate
foreach (int n in numbers)
{
Console.Write($"{n} ");
}
Console.WriteLine();
LINQ basics
using System;
using System.Linq;
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Filter
var evens = nums.Where(n => n % 2 == 0);
Console.WriteLine($"Evens: {string.Join(", ", evens)}");
// Transform
var doubled = nums.Select(n => n * 2);
Console.WriteLine($"Doubled: {string.Join(", ", doubled)}");
// Aggregate
Console.WriteLine($"Sum: {nums.Sum()}");
Console.WriteLine($"Average: {nums.Average()}");
Console.WriteLine($"Min: {nums.Min()}");
Console.WriteLine($"Max: {nums.Max()}");
// Order
var desc = nums.OrderByDescending(n => n);
Console.WriteLine($"Desc: {string.Join(", ", desc)}");
// First / Last
Console.WriteLine($"First: {nums.First()}");
Console.WriteLine($"Last: {nums.Last()}");
Console.WriteLine($"First > 5: {nums.First(n => n > 5)}");
Dictionary<TKey, TValue>
using System;
using System.Collections.Generic;
var ages = new Dictionary<string, int>
{
["Alice"] = 30,
["Bob"] = 25,
["Charlie"] = 35
};
// Access
Console.WriteLine($"Alice: {ages["Alice"]}");
// TryGetValue (safe)
if (ages.TryGetValue("David", out int age))
{
Console.WriteLine($"David: {age}");
}
else
{
Console.WriteLine("David not found");
}
// Add and remove
ages["Eve"] = 28;
ages.Remove("Charlie");
// Iterate
foreach (var (name, a) in ages)
{
Console.WriteLine($"{name}: {a}");
}
Array vs List
| Feature | Array | List<T> |
|---|---|---|
| Size | Fixed | Dynamic |
| Performance | Faster | Slightly slower |
| Memory | Less overhead | More overhead |
| Use when | Size known at compile time | Size changes at runtime |
Mini Practice
Write C# code that:
- Creates a jagged array and prints each row
- Uses
List<T>to build a dynamic collection - Uses LINQ to filter and transform an array
- Creates a dictionary and iterates with
foreach
Up Next
In the next lesson, you'll learn about Methods — defining and using methods in C#.
Related Topics
Frequently Asked Questions about Arrays
What is Arrays in C#?
Arrays is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Arrays?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Arrays.
Why is Arrays important in C#?
Arrays is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.