C# (pronounced C-Sharp) is a modern, strongly-typed, object-oriented, and multi-paradigm programming language developed by Microsoft as part of the .NET ecosystem. It is designed for building high-performance, secure, scalable, and cross-platform applications.
1. High-Level Overview of C
Key Characteristics
- Strongly typed
- Managed language
- Garbage collected
- Compiled to Intermediate Language (IL)
- Runs on the .NET Runtime
- Cross-platform (Windows, Linux, macOS)
-
Multi-paradigm:
- Object-Oriented
- Functional
- Imperative
- Declarative
Common Use Cases
- Web applications (ASP.NET Core)
- Desktop apps (WPF, WinForms, MAUI)
- Mobile apps (Xamarin / MAUI)
- Game development (Unity)
- Cloud & microservices
- APIs & backend systems
- Desktop & enterprise software
2. C# Architecture (Big Picture)
C# Source Code (.cs)
↓
C# Compiler (Roslyn)
↓
Intermediate Language (IL)
↓
.NET Runtime (CLR / CoreCLR)
↓
JIT Compiler
↓
Native Machine Code
↓
CPU Execution
3. .NET Platform Architecture
Core Components
- C# Compiler (Roslyn)
- Common Language Runtime (CLR / CoreCLR)
- Base Class Library (BCL)
- Just-In-Time Compiler (JIT)
- Garbage Collector (GC)
4. C# Compilation Process (Internal)
Step-by-Step
-
.csfiles are compiled by Roslyn - Output is IL (Intermediate Language)
- IL stored inside Assemblies (
.exeor.dll) - CLR loads assemblies
- JIT converts IL to native code
- Native code executed by CPU
5. CLR – Common Language Runtime (Internals)
CLR provides:
- Memory management
- Garbage collection
- Type safety
- Security
- Exception handling
- Thread management
- Interoperability
6. Memory Model in C
Memory Areas
- Stack – value types, method calls
- Heap – reference types
- Managed Heap – GC-controlled
- LOH (Large Object Heap) – objects > 85 KB
Value vs Reference Types
int x = 10; // Value type (stack)
string s = "Hello"; // Reference type (heap)
7. Garbage Collection (GC)
GC Generations
- Generation 0 – short-lived objects
- Generation 1 – medium-lived
- Generation 2 – long-lived
GC automatically:
- Allocates memory
- Frees unused objects
- Compacts memory
8. Basic Program Structure
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, C#");
}
}
9. Variables and Data Types
Built-in Value Types
int a = 10;
float b = 5.5f;
double c = 3.14;
bool flag = true;
char ch = 'A';
Reference Types
string name = "C#";
object obj = name;
10. Constants and Readonly
const int MAX = 100;
readonly int id;
11. Operators
Arithmetic
+, -, *, /, %
Logical
&&, ||, !
Bitwise
&, |, ^, <<, >>
12. Control Flow Statements
if / else
if (x > 0)
{
Console.WriteLine("Positive");
}
else
{
Console.WriteLine("Negative");
}
switch
switch (day)
{
case 1: Console.WriteLine("Monday"); break;
default: Console.WriteLine("Unknown"); break;
}
13. Loops
for
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
while
while (x > 0)
{
x--;
}
foreach
foreach (var item in list)
{
Console.WriteLine(item);
}
14. Methods and Functions
static int Add(int a, int b)
{
return a + b;
}
Method Overloading
int Add(int a, int b);
double Add(double a, double b);
15. Parameters
void Test(ref int x) { }
void Test(out int x) { x = 10; }
void Test(in int x) { }
16. Classes and Objects
class Person
{
public string Name;
public int Age;
public void Speak()
{
Console.WriteLine(Name);
}
}
17. Constructors and Destructors
class Car
{
public Car()
{
Console.WriteLine("Created");
}
~Car()
{
Console.WriteLine("Destroyed");
}
}
18. Encapsulation (Access Modifiers)
public
private
protected
internal
protected internal
private protected
19. Inheritance
class Animal
{
public void Eat() { }
}
class Dog : Animal
{
public void Bark() { }
}
20. Polymorphism
Virtual and Override
class Base
{
public virtual void Show() { }
}
class Derived : Base
{
public override void Show() { }
}
21. Abstract Classes
abstract class Shape
{
public abstract double Area();
}
22. Interfaces
interface ILogger
{
void Log(string message);
}
23. Structs
struct Point
{
public int X;
public int Y;
}
24. Enums
enum Status
{
Pending,
Approved,
Rejected
}
25. Properties
class User
{
public string Name { get; set; }
}
26. Records (Immutable Types)
record Person(string Name, int Age);
27. Delegates
delegate void MyDelegate(string msg);
28. Events
event EventHandler MyEvent;
29. Lambda Expressions
(x, y) => x + y;
30. LINQ (Language Integrated Query)
var result = numbers.Where(n => n > 5).Select(n => n * 2);
31. Collections
List<int>
Dictionary<TKey, TValue>
Queue<T>
Stack<T>
HashSet<T>
32. Exception Handling
try
{
int x = 10 / 0;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
}
33. Async & Await (Concurrency)
async Task<int> GetDataAsync()
{
await Task.Delay(1000);
return 10;
}
34. Multithreading
Thread t = new Thread(() => Console.WriteLine("Thread"));
t.Start();
35. File I/O
File.WriteAllText("test.txt", "Hello");
string data = File.ReadAllText("test.txt");
36. Reflection
Type t = typeof(string);
Console.WriteLine(t.FullName);
37. Attributes
[Obsolete("Do not use")]
void OldMethod() { }
38. Unsafe Code (Low-Level)
unsafe
{
int x = 10;
int* p = &x;
}
39. Interoperability (C++ / Native)
- P/Invoke
- COM Interop
- Native DLL calls
40. C# vs C / C++ (Internal Difference)
| Feature | C# | C++ |
|---|---|---|
| Memory | Managed | Manual |
| GC | Yes | No |
| Platform | Cross-platform | Platform-dependent |
| Safety | High | Low |
41. C# Execution Model Summary
- Managed execution
- JIT compilation
- Garbage collection
- Strong type system
- Runtime safety
42. When to Use C
- Enterprise systems
- Web APIs
- Cross-platform apps
- Game development
- High-level system software
Final Words
C# is not just a language, it is a complete application platform backed by one of the most advanced runtimes ever built.
From low-level memory control to high-level functional programming, C# scales from small scripts to massive distributed systems.
If you master C#, you master modern software engineering.
Top comments (0)