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
| Characteristic | Description |
|---|---|
| Strongly-typed | Every variable and expression has a type known at compile time |
| Object-oriented | Supports encapsulation, inheritance, polymorphism, and abstraction |
| Type-safe | Prevents unsafe casts and guarantees memory safety |
| Garbage-collected | Automatic memory management via the .NET GC |
| Managed | Runs on the CLR; the runtime handles memory, security, and threading |
| Component-oriented | Properties, 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:
usingdirectives — 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#.
Mainmethod — the application entry point. Can returnvoidorint.
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.
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:
- Source code (
.csfiles) is compiled by Roslyn (the C# compiler). - Roslyn emits Intermediate Language (IL) assembled into a
.dllor.exe. - At runtime, the CLR (Common Language Runtime) uses the JIT compiler to convert IL to native machine code.
- The native code executes on the processor.
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
| Version | Year | Key Features |
|---|---|---|
| C# 1.0 | 2002 | Managed code, classes, structs, interfaces, delegates, events |
| C# 2.0 | 2005 | Generics, partial classes, nullable types, anonymous methods |
| C# 3.0 | 2007 | LINQ, lambda expressions, extension methods, anonymous types, auto-properties |
| C# 4.0 | 2010 | Dynamic binding (dynamic), named/optional arguments, covariance |
| C# 5.0 | 2012 | async/await, caller info attributes |
| C# 6.0 | 2015 | String interpolation, null-conditional operator, expression-bodied members |
| C# 7.0–7.3 | 2017–18 | Pattern matching, tuples, out variables, local functions, ref return |
| C# 8.0 | 2019 | Nullable reference types, switch expressions, async streams, indices/ranges |
| C# 9.0 | 2020 | Top-level statements, records, init-only setters, pattern matching enhancements |
| C# 10.0 | 2021 | Global usings, file-scoped namespaces, record structs, constant interpolated strings |
| C# 11.0 | 2022 | Raw string literals, required members, generic math, list patterns |
| C# 12.0 | 2023 | Primary constructors for classes, collection expressions, inline arrays |
| C# 13.0 | 2024 | params 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
| Scenario | Recommendation |
|---|---|
| Web APIs & backends | C# + ASP.NET Core — industry standard |
| Desktop applications | WPF, WinForms, or MAUI |
| Game development | Unity uses C# as its scripting language |
| Cloud / serverless | Azure Functions, AWS Lambda support C# |
| Mobile apps | .NET MAUI (cross-platform) |
| Learning OOP concepts | C# 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
- C# is a modern, multi-paradigm language designed by Microsoft and standardized by ECMA.
- It compiles to IL (Intermediate Language) which the CLR JIT-compiles to native code at runtime.
- 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).
- 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, andasync/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.