C# dev-docs

Welcome to the C# development docs
Helpful .NET summaries and practical examples.
→ Official C# Documentation

Object-Oriented Programming (OOP)

Three core principles of OOP in C#:
1. Abstraction – Hide complexity using abstract classes or interfaces
2. Encapsulation – Protect internal state using access modifiers
3. Inheritance – Extend functionality from base classes

Comments

// Single-line comment
/*
Multi-line
comment
*/
// TODO: Task marker
/// XML comment for documentation

Data Types

Data TypeSizeRange
int4 bytes−2^31 to 2^31−1
long8 bytes−2^63 to 2^63−1
float4 bytes~6–7 decimal digits
double8 bytes~15 decimal digits
decimal16 bytes~28–29 digits (financial)
char2 bytesUnicode (0–65535)
bool1 bittrue / false
string2 bytes per charVariable length

Variables

int num = 999;
string word = "Hello";
bool isReady = true;
double price = 19.99;

Basic Syntax

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public void SayHello()
{
    Console.WriteLine("Hello!");
}

Control Structures

// If-Else
if (condition)
{
    // true branch
}
else
{
    // false branch
}

// For loop
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}

// While loop
int j = 0;
while (j < 10)
{
    Console.WriteLine(j);
    j++;
}

Methods

public int Add(int a, int b)
{
    return a + b;
}

int result = Add(5, 10);
Console.WriteLine(result); // 15

Properties

public class Car
{
    public string Color { get; set; }
    public string Model { get; set; }
}

Car myCar = new Car();
myCar.Color = "Red";
Console.WriteLine(myCar.Color);

Arrays

int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[0]); // 1

Lists

using System.Collections.Generic;

List names = new List();
names.Add("Alice");
names.Add("Bob");

foreach (var name in names)
{
    Console.WriteLine(name);
}

Dictionaries

using System.Collections.Generic;

var scores = new Dictionary();
scores["Alice"] = 90;
scores["Bob"] = 85;

foreach (var entry in scores)
{
    Console.WriteLine($"{entry.Key}: {entry.Value}");
}

LINQ Examples

Aggregate File Statistics

var files = await context.Files.ToListAsync();

int fileCount = files
    .Select(f => Path.GetFileName(f.Path))
    .Distinct()
    .Count();

int folderCount = files
    .Select(f => Path.GetDirectoryName(f.Path))
    .Distinct()
    .Count();

long totalBytes = files.Sum(f => f.SizeInBytes);

Console.WriteLine($"Files: {fileCount}, Folders: {folderCount}");
Console.WriteLine($"Total Size: {FormatSize(totalBytes)}");

Convert File Size

public string FormatSize(long bytes)
{
    double unit = 1000;
    if (bytes < unit) return $"{bytes} B";
    if (bytes < unit * unit) return $"{bytes / unit:F2} KB";
    if (bytes < unit * unit * unit) return $"{bytes / (unit * unit):F2} MB";
    if (bytes < unit * unit * unit * unit) return $"{bytes / (unit * unit * unit):F2} GB";
    return $"{bytes / (unit * unit * unit * unit):F2} TB";
}

Top 10 Directories by File Size

var topDirs = files
    .GroupBy(f => Path.GetDirectoryName(f.Path))
    .Select(g => new
    {
        Directory = g.Key,
        TotalSize = g.Sum(f => f.SizeInBytes)
    })
    .OrderByDescending(g => g.TotalSize)
    .Take(10);

foreach (var dir in topDirs)
{
    Console.WriteLine($"{FormatSize(dir.TotalSize)} - {dir.Directory}");
}