Master arrays, methods, and string manipulation in C#. Build practical skills for .NET development.
Learn to work with collections and create reusable code blocks.
int[] numbers = {10, 25, 8, 42, 15}; - Array initializer (most common)int[] numbers = new int[5]; - Creates array with default values (0)int[] numbers = new int[] {10, 25, 8, 42, 15}; - Explicit new keyword.Length property: numbers.Length returns the number of elements. This is a property, not a method, so no parentheses!numbers[0] → First element (10)numbers[4] → Fifth element (15)numbers[numbers.Length - 1] → Last elementforeach loop (preferred) or traditional for loop:foreach (int num in numbers) { Console.WriteLine(num); }for (int i = 0; i < numbers.Length; i++) { ... }List<int> instead of arraysArray.Sort(numbers) to sort in-placeArray.Reverse(numbers) to reverse ordernumbers.Sum(), numbers.Average(), numbers.Max()Array length: 5
First element: 10
Last element: 15
All elements: 10 25 8 42 15
Sum: 100
Average: 20
int[] numbers = {10, 25, 8, 42, 15};numbers.Length (property, no parentheses)numbers[0]numbers[numbers.Length - 1]foreach (int n in numbers) { }(double)sum / numbers.LengthReview feedback below
static int Add(int a, int b)
{
return a + b;
}
• static - Method belongs to the class, not an instanceint - Return type (what the method gives back)Add - Method name (use PascalCase in C#)(int a, int b) - Parameters (inputs)static void Greet(string name)
{
Console.WriteLine($"Hello, {name}!");
}
• void - No return value (just performs an action)static int Multiply(int a, int b) { return a * b; }
static int Multiply(int a, int b, int c) { return a * b * c; }static void PrintMessage(string msg, int times = 1)
{
for (int i = 0; i < times; i++)
Console.WriteLine(msg);
}CalculateTotal() not Calc()out or ref parameters for multiple returnsSum: 15
Hello, Alice!
Product of 2 numbers: 20
Product of 3 numbers: 60
Message printed once
Message printed 3 times
static int Add(int a, int b) { return a + b; }static void Greet(string name) { ... }int result = Add(5, 3);Review feedback below
string message = "Hello, C# World!";
int length = message.Length;string upper = message.ToUpper(); // "HELLO, C# WORLD!"
string lower = message.ToLower(); // "hello, c# world!"
Note: These methods return NEW strings (strings are immutable in C#).string sub1 = message.Substring(0, 5); // "Hello" (start, length)
string sub2 = message.Substring(7); // "C# World!" (from index 7 to end)int index = message.IndexOf("C#"); // 7 (position of "C#")
bool contains = message.Contains("World"); // true
bool starts = message.StartsWith("Hello"); // true
bool ends = message.EndsWith("!"); // truestring replaced = message.Replace("World", "Universe");
string trimmed = " spaces ".Trim(); // "spaces"string[] words = message.Split(' '); // Split by space
string joined = string.Join("-", words); // Join with dashStringBuilder for many concatenations (better performance)string.IsNullOrEmpty(str) checks for null or ""string.IsNullOrWhiteSpace(str) also checks for whitespace@"string" for verbatim strings (ignores escape chars)Original: Hello, C# World!
Length: 16
Uppercase: HELLO, C# WORLD!
Lowercase: hello, c# world!
Substring: Hello
Contains 'C#': True
Index of 'World': 10
Replaced: Hello, C# Universe!
message.Length (property)message.ToUpper()message.Substring(start, length)message.Contains("text")message.Replace("old", "new")Review feedback below