DEV Community

Cover image for A Complete Introduction to C# — The Modern, Powerful, Multi-Paradigm Language for Developers
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on • Edited on

A Complete Introduction to C# — The Modern, Powerful, Multi-Paradigm Language for Developers

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
Enter fullscreen mode Exit fullscreen mode

3. .NET Platform Architecture

Core Components

  1. C# Compiler (Roslyn)
  2. Common Language Runtime (CLR / CoreCLR)
  3. Base Class Library (BCL)
  4. Just-In-Time Compiler (JIT)
  5. Garbage Collector (GC)

4. C# Compilation Process (Internal)

Step-by-Step

  1. .cs files are compiled by Roslyn
  2. Output is IL (Intermediate Language)
  3. IL stored inside Assemblies (.exe or .dll)
  4. CLR loads assemblies
  5. JIT converts IL to native code
  6. 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)
Enter fullscreen mode Exit fullscreen mode

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#");
    }
}
Enter fullscreen mode Exit fullscreen mode

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';
Enter fullscreen mode Exit fullscreen mode

Reference Types

string name = "C#";
object obj = name;
Enter fullscreen mode Exit fullscreen mode

10. Constants and Readonly

const int MAX = 100;

readonly int id;
Enter fullscreen mode Exit fullscreen mode

11. Operators

Arithmetic

+, -, *, /, %
Enter fullscreen mode Exit fullscreen mode

Logical

&&, ||, !
Enter fullscreen mode Exit fullscreen mode

Bitwise

&, |, ^, <<, >>
Enter fullscreen mode Exit fullscreen mode

12. Control Flow Statements

if / else

if (x > 0)
{
    Console.WriteLine("Positive");
}
else
{
    Console.WriteLine("Negative");
}
Enter fullscreen mode Exit fullscreen mode

switch

switch (day)
{
    case 1: Console.WriteLine("Monday"); break;
    default: Console.WriteLine("Unknown"); break;
}
Enter fullscreen mode Exit fullscreen mode

13. Loops

for

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}
Enter fullscreen mode Exit fullscreen mode

while

while (x > 0)
{
    x--;
}
Enter fullscreen mode Exit fullscreen mode

foreach

foreach (var item in list)
{
    Console.WriteLine(item);
}
Enter fullscreen mode Exit fullscreen mode

14. Methods and Functions

static int Add(int a, int b)
{
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Method Overloading

int Add(int a, int b);
double Add(double a, double b);
Enter fullscreen mode Exit fullscreen mode

15. Parameters

void Test(ref int x) { }
void Test(out int x) { x = 10; }
void Test(in int x) { }
Enter fullscreen mode Exit fullscreen mode

16. Classes and Objects

class Person
{
    public string Name;
    public int Age;

    public void Speak()
    {
        Console.WriteLine(Name);
    }
}
Enter fullscreen mode Exit fullscreen mode

17. Constructors and Destructors

class Car
{
    public Car()
    {
        Console.WriteLine("Created");
    }

    ~Car()
    {
        Console.WriteLine("Destroyed");
    }
}
Enter fullscreen mode Exit fullscreen mode

18. Encapsulation (Access Modifiers)

public
private
protected
internal
protected internal
private protected
Enter fullscreen mode Exit fullscreen mode

19. Inheritance

class Animal
{
    public void Eat() { }
}

class Dog : Animal
{
    public void Bark() { }
}
Enter fullscreen mode Exit fullscreen mode

20. Polymorphism

Virtual and Override

class Base
{
    public virtual void Show() { }
}

class Derived : Base
{
    public override void Show() { }
}
Enter fullscreen mode Exit fullscreen mode

21. Abstract Classes

abstract class Shape
{
    public abstract double Area();
}
Enter fullscreen mode Exit fullscreen mode

22. Interfaces

interface ILogger
{
    void Log(string message);
}
Enter fullscreen mode Exit fullscreen mode

23. Structs

struct Point
{
    public int X;
    public int Y;
}
Enter fullscreen mode Exit fullscreen mode

24. Enums

enum Status
{
    Pending,
    Approved,
    Rejected
}
Enter fullscreen mode Exit fullscreen mode

25. Properties

class User
{
    public string Name { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

26. Records (Immutable Types)

record Person(string Name, int Age);
Enter fullscreen mode Exit fullscreen mode

27. Delegates

delegate void MyDelegate(string msg);
Enter fullscreen mode Exit fullscreen mode

28. Events

event EventHandler MyEvent;
Enter fullscreen mode Exit fullscreen mode

29. Lambda Expressions

(x, y) => x + y;
Enter fullscreen mode Exit fullscreen mode

30. LINQ (Language Integrated Query)

var result = numbers.Where(n => n > 5).Select(n => n * 2);
Enter fullscreen mode Exit fullscreen mode

31. Collections

List<int>
Dictionary<TKey, TValue>
Queue<T>
Stack<T>
HashSet<T>
Enter fullscreen mode Exit fullscreen mode

32. Exception Handling

try
{
    int x = 10 / 0;
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
}
Enter fullscreen mode Exit fullscreen mode

33. Async & Await (Concurrency)

async Task<int> GetDataAsync()
{
    await Task.Delay(1000);
    return 10;
}
Enter fullscreen mode Exit fullscreen mode

34. Multithreading

Thread t = new Thread(() => Console.WriteLine("Thread"));
t.Start();
Enter fullscreen mode Exit fullscreen mode

35. File I/O

File.WriteAllText("test.txt", "Hello");
string data = File.ReadAllText("test.txt");
Enter fullscreen mode Exit fullscreen mode

36. Reflection

Type t = typeof(string);
Console.WriteLine(t.FullName);
Enter fullscreen mode Exit fullscreen mode

37. Attributes

[Obsolete("Do not use")]
void OldMethod() { }
Enter fullscreen mode Exit fullscreen mode

38. Unsafe Code (Low-Level)

unsafe
{
    int x = 10;
    int* p = &x;
}
Enter fullscreen mode Exit fullscreen mode

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)