Welcome to the C# development docs
Helpful .NET summaries and practical examples.
→ Official C# Documentation
1. Abstraction – Hide complexity using abstract classes or interfaces
2. Encapsulation – Protect internal state using access modifiers
3. Inheritance – Extend functionality from base classes
// Single-line comment
/*
Multi-line
comment
*/
// TODO: Task marker
/// XML comment for documentation
| Data Type | Size | Range |
|---|---|---|
| int | 4 bytes | −2^31 to 2^31−1 |
| long | 8 bytes | −2^63 to 2^63−1 |
| float | 4 bytes | ~6–7 decimal digits |
| double | 8 bytes | ~15 decimal digits |
| decimal | 16 bytes | ~28–29 digits (financial) |
| char | 2 bytes | Unicode (0–65535) |
| bool | 1 bit | true / false |
| string | 2 bytes per char | Variable length |
int num = 999;
string word = "Hello";
bool isReady = true;
double price = 19.99;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public void SayHello()
{
Console.WriteLine("Hello!");
}
// 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++;
}
public int Add(int a, int b)
{
return a + b;
}
int result = Add(5, 10);
Console.WriteLine(result); // 15
public class Car
{
public string Color { get; set; }
public string Model { get; set; }
}
Car myCar = new Car();
myCar.Color = "Red";
Console.WriteLine(myCar.Color);
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[0]); // 1
using System.Collections.Generic;
List names = new List();
names.Add("Alice");
names.Add("Bob");
foreach (var name in names)
{
Console.WriteLine(name);
}
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}");
}
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)}");
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";
}
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}");
}