Skip to main content

C# Overview

Definition

C# (pronounced "C sharp") is a modern, general-purpose, object-oriented programming language designed by Anders Hejlsberg at Microsoft and released in 2000 as part of the .NET initiative. It is standardized under ECMA-334 and ISO/IEC 23270. C# is the primary language for building applications on the .NET platform — spanning web APIs, desktop apps, mobile apps, games (Unity), cloud services, and more.

Core Concepts

Key Characteristics

CharacteristicDescription
Strongly-typedEvery variable and expression has a type known at compile time
Object-orientedSupports encapsulation, inheritance, polymorphism, and abstraction
Type-safePrevents unsafe casts and guarantees memory safety
Garbage-collectedAutomatic memory management via the .NET GC
ManagedRuns on the CLR; the runtime handles memory, security, and threading
Component-orientedProperties, events, attributes, and delegates support component-based design

Program Structure

A traditional C# program is organized into namespaces, classes, and methods:

using System;                       // Using directive — imports a namespace

namespace MyApplication // Namespace — organizes types
{
class Program // Class — container for data and behavior
{
static void Main(string[] args) // Entry point — where execution begins
{
Console.WriteLine("Hello, World!");
}
}
}

Key structural elements:

  • using directives — import namespaces so you don't need fully qualified names.
  • Namespaces — logical grouping of related types.
  • Classes — blueprints for objects; the fundamental unit of OOP in C#.
  • Main method — the application entry point. Can return void or int.

Top-Level Statements (C# 9+)

Since C# 9, you can write a program without the boilerplate class and Main method:

// This IS a complete C# program (C# 9+)
using System;

Console.WriteLine("Hello, World!");

The compiler generates the Program class and Main method automatically. Only one file in a project can use top-level statements.

When to use top-level statements

Use top-level statements for simple console apps, scripts, and learning. For larger applications with multiple types, the traditional namespace/class structure is clearer.

Compilation Process

C# code goes through several stages before it executes:

  1. Source code (.cs files) is compiled by Roslyn (the C# compiler).
  2. Roslyn emits Intermediate Language (IL) assembled into a .dll or .exe.
  3. At runtime, the CLR (Common Language Runtime) uses the JIT compiler to convert IL to native machine code.
  4. The native code executes on the processor.
Managed vs Unmanaged Code

Code that runs on the CLR is called managed code — the runtime handles memory (GC), type safety, and security. Unmanaged code (e.g., C/C++ via P/Invoke) runs outside the CLR and is not subject to these safeguards.

C# Version History Highlights

VersionYearKey Features
C# 1.02002Managed code, classes, structs, interfaces, delegates, events
C# 2.02005Generics, partial classes, nullable types, anonymous methods
C# 3.02007LINQ, lambda expressions, extension methods, anonymous types, auto-properties
C# 4.02010Dynamic binding (dynamic), named/optional arguments, covariance
C# 5.02012async/await, caller info attributes
C# 6.02015String interpolation, null-conditional operator, expression-bodied members
C# 7.0–7.32017–18Pattern matching, tuples, out variables, local functions, ref return
C# 8.02019Nullable reference types, switch expressions, async streams, indices/ranges
C# 9.02020Top-level statements, records, init-only setters, pattern matching enhancements
C# 10.02021Global usings, file-scoped namespaces, record structs, constant interpolated strings
C# 11.02022Raw string literals, required members, generic math, list patterns
C# 12.02023Primary constructors for classes, collection expressions, inline arrays
C# 13.02024params collections, ref struct improvements, partial properties

How C# Relates to .NET

C# is a language; .NET is the platform on which it runs.

  • CLR (Common Language Runtime) — the execution engine (GC, JIT, type system).
  • BCL (Base Class Library) — the standard library providing collections, I/O, threading, etc.
  • SDK — tools and compilers for building .NET applications.
  • Other languages (F#, VB.NET) also compile to IL and run on the same CLR.

Code Examples

Traditional Program Structure

using System;
using System.Collections.Generic;

namespace OrderProcessing
{
class Program
{
static int Main(string[] args)
{
var orders = new List<string> { "Order-001", "Order-002" };

foreach (var order in orders)
{
Console.WriteLine($"Processing {order}");
}

return 0; // Exit code
}
}
}

Top-Level Statements (C# 9+)

using System;
using System.Collections.Generic;

var orders = new List<string> { "Order-001", "Order-002" };

orders.ForEach(order => Console.WriteLine($"Processing {order}"));

File-Scoped Namespace (C# 10+)

namespace OrderProcessing; // File-scoped — applies to entire file

class OrderService
{
public void Process(string orderId)
{
Console.WriteLine($"Processing {orderId}");
}
}

When to Use

ScenarioRecommendation
Web APIs & backendsC# + ASP.NET Core — industry standard
Desktop applicationsWPF, WinForms, or MAUI
Game developmentUnity uses C# as its scripting language
Cloud / serverlessAzure Functions, AWS Lambda support C#
Mobile apps.NET MAUI (cross-platform)
Learning OOP conceptsC# is clean, well-documented, and beginner-friendly

Common Pitfalls

  • Confusing C# and .NET — C# is a language; .NET is the runtime and library. You can write .NET apps in F# or VB.NET.
  • Assuming C# is the same as Java — While syntactically similar, C# has properties, delegates, value types (structs), LINQ, nullable reference types, and many features Java lacks or added later.
  • Ignoring nullable reference types (C# 8+) — Enable <Nullable>enable</Nullable> in new projects. It catches null-related bugs at compile time.
  • Using old-style patterns — Prefer expression-bodied members, pattern matching, and switch expressions over verbose C# 1.0–5.0 patterns.

Key Takeaways

  1. C# is a modern, multi-paradigm language designed by Microsoft and standardized by ECMA.
  2. It compiles to IL (Intermediate Language) which the CLR JIT-compiles to native code at runtime.
  3. The language has evolved significantly — from a Java-like OOP language (C# 1.0) to a modern language with pattern matching, async, and functional features (C# 13).
  4. C# is the primary (but not the only) language of the .NET ecosystem.

Interview Questions

Q: What is C# and how does it differ from Java?

C# is a modern, object-oriented, type-safe language by Microsoft running on the .NET CLR. Key differences from Java: C# has properties (no getter/setter methods), value types (struct), delegates and events, LINQ, nullable reference types, operator overloading, and async/await (which Java added later as virtual threads). C# also has a more rapid release cadence.

Q: What is managed code?

Managed code is code that runs on the CLR. The runtime provides automatic memory management (garbage collection), type safety verification, exception handling, and security services. Unmanaged code (e.g., C++ via P/Invoke) runs outside the CLR without these protections.

Q: What is the difference between C# and .NET?

C# is a programming language — syntax, keywords, language features. .NET is the platform: the runtime (CLR), the base class library (BCL), and the SDK/tooling. C# compiles to IL that the .NET runtime executes. Other languages like F# and VB.NET also target .NET.

References