Core PlatformComplete guide

C#

C# is the primary general-purpose language of .NET. It combines static typing, managed memory, object-oriented and functional techniques, expressive data modeling, asynchronous control flow, and access to the full .NET library ecosystem.

Overview

Production C# is less about using every syntax feature and more about making contracts, ownership, state changes, failure modes, and concurrency visible. Types should prevent invalid states where practical; nullable annotations should match runtime truth; exceptions should represent exceptional failure; async code must propagate cancellation; LINQ must be understood as an execution pipeline; and newer language features should improve clarity without hiding cost. C# 14 on .NET 10 adds extension members, field-backed properties, null-conditional assignment, and improved Span conversions, while the durable fundamentals remain types, generics, patterns, delegates, resource ownership, and testable boundaries.

What you will learn

  • Distinguish value types, reference types, identity, value equality, boxing, copying, and nullable intent.
  • Design domain contracts with classes, records, record structs, interfaces, generics, and constrained type parameters.
  • Use pattern matching, expressions, delegates, lambdas, and LINQ while keeping control flow and execution cost visible.
  • Build asynchronous and streaming workflows with Task, ValueTask, IAsyncEnumerable, cancellation, and exception propagation.
  • Own managed and unmanaged resources with IDisposable, IAsyncDisposable, using, and clear lifetime boundaries.
  • Apply analyzers, tests, benchmarks, and API-compatibility discipline to evolve production C# safely.

Prerequisites

  • Basic programming concepts such as variables, conditions, loops, methods, collections, and exceptions.
  • The .NET 10 SDK and a terminal; the dedicated sample has no database or external-service dependency.
  • Familiarity with projects, namespaces, packages, build warnings, and automated tests is helpful.

When to use C#

Good fit

  • Web, cloud, desktop, mobile, data, tooling, games, and services that run on .NET and benefit from strong tooling and static analysis.
  • Domains where expressive types, generics, records, pattern matching, and nullable contracts can make rules reviewable and testable.
  • Concurrent I/O-heavy systems that benefit from Task-based APIs, async streams, cancellation, and mature runtime diagnostics.

Choose another approach when

  • A host where neither the .NET runtime nor an appropriate native AOT artifact can meet platform, startup, memory, or deployment constraints.
  • A team or ecosystem whose required libraries, operational tooling, or platform ownership strongly favors another supported language.
  • Using dynamic or reflection-heavy C# to imitate an untyped language when compile-time contracts would be clearer and safer.

Key concepts

Types, identity, and equality

Value types contain their data and are copied by value; reference types carry object references. Equality semantics, mutability, allocation, and boxing should match the domain.

Nullable reference analysis

Annotations such as string and string? express intent while flow analysis warns when maybe-null values are assigned or dereferenced; runtime representation is unchanged.

Classes, records, and structs

Classes suit identity and behavior, records provide value-oriented equality and with expressions, and small readonly structs can model allocation-free values when copying is cheap.

Generics and constraints

Generic types and methods preserve compile-time information and avoid casts; constraints describe the members, construction, nullability, or value/reference behavior an algorithm requires.

Patterns and expressions

Type, property, relational, list, logical, and recursive patterns make branching over shape and state explicit; guards handle rules not expressible as patterns alone.

Delegates, lambdas, and LINQ

Delegates model executable behavior, lambdas create it concisely, and LINQ composes transformations whose deferred or provider-specific execution must remain understood.

Tasks, async streams, and cancellation

Task represents eventual completion, async and await preserve readable control flow, IAsyncEnumerable streams results, and CancellationToken carries cooperative cancellation.

Resource and failure ownership

Exceptions communicate exceptional failure, result types can model expected alternatives, and using or await using defines deterministic cleanup for scarce resources.

Architecture

External data is parsed and validated into typed boundary contracts. Application workflows coordinate domain behavior through interfaces and explicit results. Infrastructure owns I/O and resources, while output contracts prevent internal mutable state from leaking.

Untrusted input
Validated contracts
Domain types + rules
Application workflow
Infrastructure adapters
Output contracts

Conceptual architecture · Adapt to your system context

Implementation walkthrough

Start with the domain language and failure boundaries, then select C# features that make those decisions obvious to callers and verifiable in tests.

  1. 01
    Enable compiler guardrails

    Select the supported language version, enable nullable analysis and analyzers, treat reviewed warnings as errors, and centralize repository policy.

  2. 02
    Model valid domain states

    Choose classes for identity, records for value-oriented contracts, and small readonly structs for cheap values; validate construction and expose only required mutation.

  3. 03
    Define boundaries and variance

    Use interfaces and generics at real substitution points, apply the narrowest useful constraints, and avoid abstraction that only renames a concrete type.

  4. 04
    Express decisions clearly

    Use pattern matching, switch expressions, guard clauses, and named methods so business branches are exhaustive, ordered, and easy to review.

  5. 05
    Compose data deliberately

    Use LINQ for readable transformations, distinguish IEnumerable from IQueryable, know when execution is deferred, and materialize once at ownership boundaries.

  6. 06
    Design asynchronous flow

    Return Task or ValueTask deliberately, avoid blocking waits, propagate CancellationToken, stream large results with IAsyncEnumerable, and preserve exception context.

  7. 07
    Test contracts and costs

    Test equality, nullability assumptions, patterns, cancellation, disposal, enumeration, serialization, public API compatibility, and measured hot paths.

Code example

The dedicated sample combines records, a readonly value object, C# 14 extension members, property patterns, LINQ, async streams, and cancellation in one typed workflow.

OrderProcessor.cs
public sealed class OrderProcessor(decimal reviewThreshold)
{
    public NextAction Decide(Order order) => order switch
    {
        { Status: OrderStatus.Pending, Total.Amount: var total }
            when total >= reviewThreshold => NextAction.ManualReview,
        { Status: OrderStatus.Pending } => NextAction.CapturePayment,
        { Status: OrderStatus.Paid } => NextAction.Fulfil,
        _ => NextAction.NoAction
    };

    public async Task<IReadOnlyList<OrderDecision>> ProcessAsync(
        IAsyncEnumerable<Order> orders,
        CancellationToken cancellationToken = default)
    {
        var decisions = new List<OrderDecision>();
        await foreach (var order in orders.WithCancellation(cancellationToken))
        {
            decisions.Add(new(order.Id, order.Total, Decide(order)));
        }

        return decisions.OrderByDescending(x => x.Total.Amount).ToArray();
    }
}

Testing strategy

Test semantics that syntax can obscure: equality, copying, null-state assumptions, enumeration timing, cancellation, exceptions, resource cleanup, and concurrency.

  • Write focused unit tests for domain construction, record equality, value-object operators, generic algorithms, pattern branches, and exhaustive fallbacks.
  • Test async methods by awaiting them; use controllable fakes or TaskCompletionSource instead of sleeps, and assert cancellation separately from failure.
  • Enumerate deferred LINQ queries intentionally and test for multiple enumeration, ordering, provider translation, and mutation of the underlying source.
  • Exercise IDisposable and IAsyncDisposable ownership on success, failure, and cancellation; verify resources are not captured beyond their intended lifetime.
  • Run analyzers and nullable warnings in CI, add integration tests at serialization and I/O boundaries, and benchmark only measured hot paths.

Security considerations

Language safety reduces classes of defects but does not validate untrusted data, authorize actions, erase secrets, or make unsafe/native boundaries harmless.

  • Validate length, range, format, encoding, and allowed values at trust boundaries; do not treat a successful parse or non-null annotation as authorization.
  • Avoid logging secrets and sensitive object graphs; remember strings are immutable and cannot be reliably cleared from memory.
  • Use checked arithmetic where overflow changes security or money decisions, bound collection growth, and reject attacker-controlled recursion or allocation sizes.
  • Treat reflection, dynamic loading, expression compilation, unsafe code, native interop, and deserialization as explicit reviewed trust boundaries.
  • Preserve exception context internally but return safe external errors; do not expose stack traces, filesystem paths, connection details, or secret-bearing values.

Performance and scalability

C# syntax maps to allocations, copies, dispatch, enumeration, state machines, and runtime calls. Measure the complete workload before trading clarity for lower-level constructs.

  • Use BenchmarkDotNet or representative application telemetry to measure CPU, allocation, throughput, latency, and generated code in Release builds.
  • Avoid accidental boxing, closure captures, repeated enumeration, intermediate collections, string concatenation in loops, and large mutable struct copies.
  • Use arrays, spans, pooling, ref-like types, or stack allocation only when profiling identifies a hot path and lifetime rules stay clear.
  • Choose Task by default; consider ValueTask only for measured frequently synchronous paths where callers obey its consumption constraints.
  • Keep LINQ when it is clear and fast enough; replace a pipeline with loops only when measurements justify the maintenance cost.

Deployment and operations

C# source is compiled under a selected language version into .NET assemblies; runtime and publish choices can change reflection, trimming, serialization, and dynamic-code behavior.

  • Record the SDK and language version, compile Release with the same nullable and analyzer policy in CI, and avoid relying on a developer IDE's implicit settings.
  • Publish and smoke-test the actual artifact; source-level success does not prove content files, native assets, dynamic loading, or runtime compatibility.
  • Enable trimming and native AOT analyzers early when those modes are targets; test reflection, serializers, dependency injection, and expression generation.
  • Version public contracts deliberately, use API compatibility checks for libraries, and account for record constructor or required-member changes in consumers and serializers.
  • Capture symbols and source metadata for diagnostics, keep secrets external, and ensure cancellation plus graceful shutdown reach asynchronous application boundaries.

Common problems and fixes

A nullable warning is silenced but a NullReferenceException still occurs.

Likely cause: The null-forgiving operator changed compiler analysis, not runtime data, or an external boundary violated its annotation.

Fix: Trace the actual source, correct annotations and validation, add appropriate nullable attributes, and reserve ! for invariants the code truly proves.

An async workflow hangs or loses throughput.

Likely cause: Code blocks on Result or Wait, holds scarce resources across awaits, ignores cancellation, or starts unbounded concurrent work.

Fix: Await end to end, propagate tokens, bound concurrency, shorten resource ownership, and inspect task, thread-pool, and dependency telemetry.

A LINQ query runs repeatedly or changes after it was created.

Likely cause: Deferred execution re-enumerates a mutable source, or IQueryable delegates behavior to a provider.

Fix: Identify the execution boundary, materialize once where ownership changes, inspect generated provider queries, and avoid hidden side effects.

Record equality or dictionary lookup behaves unexpectedly.

Likely cause: Generated value equality includes different members than intended, mutable members changed a key, or reference equality was required.

Fix: Choose identity versus value semantics explicitly, keep keys immutable, customize equality only with a complete contract, and test hash behavior.

Code works normally but fails after trimming or native AOT publishing.

Likely cause: Reflection, dynamic code, serializers, or plugins depend on members the compiler cannot discover statically.

Fix: Act on analyzer warnings, prefer source generation and explicit registrations, annotate unavoidable reflection, and test the published artifact.

Best practices

  • Enable nullable reference analysis and correct the contract instead of routinely suppressing warnings.
  • Choose class, record, record struct, or readonly struct from identity, equality, mutation, copying, and allocation needs.
  • Make invalid states difficult to construct and validate untrusted data at the boundary.
  • Use generics and interfaces where they preserve meaningful type information or substitution, not as ceremony.
  • Keep pattern matches exhaustive and ordered from specific to general; name complex guards.
  • Understand deferred execution and materialize LINQ results once at clear ownership boundaries.
  • Await asynchronous work end to end, propagate cancellation, and bound parallelism.
  • Give scarce resources deterministic owners and preserve exception context when adding useful domain information.
  • Measure before applying spans, pooling, ValueTask, unsafe code, or allocation-avoiding complexity.

Production checklist

  • The supported SDK and C# language version are recorded; nullable analysis, analyzers, formatting, and warning policy run in CI.
  • Public types make identity, equality, nullability, mutation, ownership, and failure behavior explicit.
  • Inputs are validated at trust boundaries and sensitive values are excluded from logs, exceptions, and serialized output.
  • Async APIs are awaited end to end, cancellation is propagated, concurrency is bounded, and async void is limited to event handlers.
  • LINQ execution, materialization, provider translation, ordering, and multiple enumeration are understood and tested.
  • Disposable and async-disposable resources have clear owners on success, exception, and cancellation paths.
  • Release tests cover public contracts and published artifacts; performance changes are measured and trimming or AOT warnings are resolved.

Common interview questions

What is the difference between a class, record, and record struct?

A class is a reference type with reference equality by default. A record class is still a reference type but synthesizes value-oriented equality and nondestructive mutation. A record struct is a value type, so assignments copy its data and size matters.

Do nullable reference types prevent null at runtime?

No. They add annotations and flow-analysis warnings at compile time without changing runtime representation. External data and oblivious code still require validation.

What does deferred execution mean in LINQ?

Many operators build a pipeline that executes only when enumerated. Re-enumeration can repeat work and observe changed source data; IQueryable may translate the expression for another provider.

Task or ValueTask?

Use Task by default because it is simple and safely awaitable multiple times. Consider ValueTask only for measured, often-synchronous hot paths and document its stricter consumption rules.

Why should async void usually be avoided?

It cannot be awaited or composed and its exceptions follow the synchronization context rather than a returned task. It is mainly appropriate for event handlers.

When would you use a generic constraint?

Use a constraint when an algorithm genuinely requires capabilities such as an interface, base type, value/reference semantics, construction, unmanaged representation, or a static abstract member.

How do IEnumerable and IQueryable differ?

IEnumerable composes delegates executed by .NET over objects. IQueryable builds expression trees that a provider translates, so supported operations, execution location, and performance depend on that provider.

Dedicated GitHub sample

Clone, run, test, and modify the focused implementation created for this guide.

Verified sample

C# Language Workbench

A focused .NET 10 sample demonstrating C# 14, nullable contracts, records and value objects, extension members, pattern matching, LINQ, async streams, cancellation, validation, and deterministic tests.

C# 14.NET 10LINQxUnit
dedicated-sampletype-systempattern-matchingasync-streamstesting
Run
dotnet run --project src/CSharpLanguageWorkbench
Test
dotnet test CSharpLanguageWorkbench.slnx