Learn
Guide
C# Fundamentals
Core language features every .NET developer needs.
What is C#?
C# is a modern, statically typed, object-oriented language by Microsoft, running on the .NET runtime. Used for web APIs (ASP.NET Core), desktop (WPF/MAUI), cloud services, and games (Unity).
Types & Variables
int age = 30;
double price = 9.99;
bool active = true;
string name = "Alice";
var x = 42; // compiler infers int
Nullable Types
string? text = null; // nullable reference type
int? count = null; // nullable value type
string s = text ?? "n/a"; // null-coalescing
string? upper = text?.ToUpper(); // null-conditional
Control Flow
if (x > 0) { } else if (x == 0) { } else { }
for (int i = 0; i < 10; i++) { }
foreach (var item in list) { }
while (running) { }
switch (role)
{
case "admin": GrantAccess(); break;
default: Deny(); break;
}
Classes & Inheritance
public class Animal
{
public string Name { get; set; }
public Animal(string name) => Name = name;
public virtual string Speak() => "...";
}
public class Dog : Animal
{
public Dog(string name) : base(name) { }
public override string Speak() => "Woof!";
}
LINQ
var nums = new List<int> { 1, 2, 3, 4, 5 };
var evens = nums.Where(n => n % 2 == 0);
var mapped = nums.Select(n => n * 2);
var sum = nums.Sum(); // 15
var first = nums.FirstOrDefault(n => n > 3); // 4
Async / Await
public async Task<string> FetchAsync(string url)
{
using var client = new HttpClient();
return await client.GetStringAsync(url);
}
Use
async Task— neverasync voidexcept for event handlers.awaitsuspends the method without blocking the thread.
Key Syntax
| Feature | Example |
|---|---|
| String interpolation | $"Hello, {name}!" |
| Pattern matching | if (obj is Dog d) { } |
| Record | record Point(int X, int Y); |
| Expression body | int Square(int x) => x * x; |
| Ternary | x > 0 ? "pos" : "neg" |