C# Concepts: Arrays, Strings, Lists, and
More
1. Arrays in C#
Declaration & Initialization:
int[] numbers = new int[] { 10, 20, 30 };
Access & Loop:
for (int i = 0; i < numbers.Length; i++)
Console.WriteLine(numbers[i]);
2. Strings in C#
Declaration & Initialization:
string name = "Sakthivel";
Common Operations:
Console.WriteLine(name.Length); // Length
Console.WriteLine(name.ToUpper()); // Upper case
Console.WriteLine(name.Contains("vel")); // true
Console.WriteLine(name[0]); // First character
String Interpolation:
string city = "Coimbatore";
Console.WriteLine($"Hello, {name} from {city}!");
3. Lists in C#
Using List<int>:
List<int> numbers = new List<int>() { 1, 2, 3 };
numbers.Add(4); // Add element
numbers.Remove(2); // Remove element
foreach (int n in numbers)
Console.WriteLine(n);
Common Methods:
Add(), Remove(), Count, Clear(), Sort()
4. Dictionary in C#
Declaration & Initialization:
Dictionary<int, string> students = new Dictionary<int, string>();
students.Add(1, "John");
students.Add(2, "Jane");
Access:
Console.WriteLine(students[1]); // Output: John
Looping:
foreach (var pair in students)
Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}");
}
5. Stack in C#
Declaration:
Stack<int> stack = new Stack<int>();
stack.Push(1); // Add
stack.Push(2);
Console.WriteLine(stack.Pop()); // Output: 2 (LIFO)
6. Queue in C#
Declaration:
Queue<int> queue = new Queue<int>();
queue.Enqueue(1); // Add
queue.Enqueue(2);
Console.WriteLine(queue.Dequeue()); // Output: 1 (FIFO)
7. Class & Object in C#
Declaration:
class Car
public string brand = "Ford";
public void Honk()
Console.WriteLine("Beep!");
// Creating object
Car myCar = new Car();
Console.WriteLine(myCar.brand);
myCar.Honk();
8. Methods in C#
Declaration:
void Greet(string name)
Console.WriteLine($"Hello {name}!");
Calling:
Greet("Sakthivel");
9. LINQ in C#
Using LINQ to filter numbers:
int[] numbers = { 1, 2, 3, 4, 5, 6 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
foreach (var n in evenNumbers)
Console.WriteLine(n);