Anatomy of a C# file
C# is a modern, object-oriented, type-safe programming language in the C-family, designed for the .NET platform.
File extensions: .cs
Every part of the example below is labelled and explained. This page is one of 55 annotated tours on AnatomyOf, a free, open-source project by LunarWerx Studios.
What is inside a C# file
Using directive
Imports namespaces for use in the file.
using System; brings every public type in the System namespace (Console, String, Int32, and so on) into scope so the rest of the file can refer to them by their short name instead of the fully qualified System.Console. Using directives are conventionally listed at the very top of the file, before the namespace declaration. Modern C# (10+) also supports implicit usings, where the compiler silently adds the most common using directives for a project type, and global using declarations that apply a directive to every file in the project from one place.
Namespace declaration
Organizes code into logical, hierarchical groups.
A namespace groups related types under a dotted name such as CSharpAnatomyDemo so that two classes named the same thing can coexist as long as they live in different namespaces. Namespaces are purely organizational and, unlike Java packages, do not need to match the file or folder layout on disk. Modern C# (10+) also allows a file-scoped form, namespace CSharpAnatomyDemo; with no braces, which applies to everything below it and removes one level of indentation from the whole file.
XML documentation comment
Triple-slash comments (///) used to generate API documentation.
A comment starting with /// is an XML documentation comment. Placed directly above a type or member, tags like <summary> and <param name="..."> describe what it does and what its parameters mean; tools can extract these comments into IntelliSense tooltips and generated reference documentation. This differs from an ordinary // or /* ... */ comment, which the compiler discards entirely. With XML doc comments, enabling the <GenerateDocumentationFile> MSBuild setting turns them into a .xml file shipped alongside the compiled assembly.
Class definition
The blueprint for objects, declared with class.
C# is thoroughly object-oriented: essentially all executable code, including Main, lives inside a class or struct. public class Program declares a class named Program that can be instantiated with new and can hold fields, properties, methods, and nested types. Unlike Java, a single .cs file may declare any number of public classes, and the file name has no required relationship to any type name inside it. Classes support single inheritance from one base class plus any number of interfaces.
Field (class variable)
Stores data for the class or its instances.
A field declared directly in the class body, like private static int _counter = 0;, holds state. Without static each instance gets its own copy; with static the field belongs to the class itself and every instance shares one copy. Access modifiers (private, public, protected, internal) control visibility. C# is strongly and statically typed, so a field's type is fixed at declaration and checked by the compiler. Convention prefixes private fields with an underscore, as in _counter, to distinguish them from parameters and properties at a glance.
Main method
The entry point for a C# application (static void Main(string[] args)).
The .NET runtime starts an executable by looking for a Main method. static means it runs without an instance of the class, void (or int, to return an exit code) describes what it hands back to the operating system, and the optional string[] args parameter receives command-line arguments. Before any of this runs, the C# compiler (csc/Roslyn) translates the source into Intermediate Language (IL) stored in an assembly; the Common Language Runtime (CLR) then JIT-compiles that IL to native code at run time. Since C# 9, "top-level statements" let a file skip the explicit Main declaration entirely fo
Method definition
A reusable block of code declared with a return type and parameter list.
A method declares an access modifier, a return type, a name, and a parenthesized parameter list, e.g. public int Add(int a, int b). void means the method returns nothing; any other return type obliges every reachable code path through the method to return a value of that type. Methods are invoked on an instance (calculator.Add(5, 10)) unless declared static, in which case they belong to the class itself and are called through the class name, like IncrementCounter() inside Program. C# supports overloading (multiple methods with the same name but different parameter lists) resolved at compile ti
Method call
Executing a method on an object or class.
Console.WriteLine("Hello, C# World!"); invokes the static WriteLine method on the Console class, and calculator.Add(5, 10); invokes an instance method on the object referenced by calculator. The compiler resolves overloads by matching the argument types at the call site. Method calls can chain and nest freely, and C# supports optional and named arguments (Add(a: 5, b: 10)), letting a caller skip parameters that have defaults or clarify which value maps to which parameter.
Control flow (loop)
Executes code repeatedly (e.g., for, while, foreach).
The classic three-clause for (int i = 0; i < 3; i++) { ... } declares a loop variable, a continuation condition checked before each pass, and an update executed after each pass. C# also has while (checked up front), do...while (checked after the first pass), and foreach, which iterates directly over any IEnumerable<T> without manual indexing. break exits the nearest loop immediately, and continue skips straight to the next iteration's check. foreach is idiomatic for collections since it also disposes iterator resources correctly when the sequence implements IDisposable.
Control flow (conditional)
Executes code based on a condition (if, else if, else).
if/else if/else route execution based on a bool expression: unlike C or JavaScript, C# does not allow integers or other types to stand in for a condition, which rules out a whole class of accidental-assignment bugs (if (x = 1) is a compile error, not a silent truth value). C# also offers a switch statement and, since C# 8, switch expressions with pattern matching, which can match on type, value ranges, and destructured properties in a single concise expression.
Object instantiation
Creates a new instance of a class with new.
new Calculator() allocates memory for a fresh object on the managed heap, runs the matching constructor, and evaluates to a reference to that object. Since C# 9, target-typed new lets the type be inferred from context, as in Calculator calculator = new();. Because objects are accessed through references, assigning one variable to another copies the reference, not the object, both variables then point at the same instance. The CLR's garbage collector reclaims unreachable objects automatically; C# has no manual free or delete.
String interpolation
Embeds expressions directly inside a string with $"...".
Prefixing a string literal with $, as in $"Counter value: {_counter}", lets any expression inside { } be evaluated and formatted into the resulting string at that position. It compiles down to a call to string.Format (or, for simple cases, direct string concatenation), but reads far more clearly than either. Interpolated strings can include format specifiers, e.g. {price:C} for currency, and can be combined with verbatim strings as $@"..." to also disable escape-sequence processing for things like file paths.
Return statement
Exits a method and, optionally, sends a value back to the caller.
return a + b; immediately ends execution of the enclosing method and hands the given value back to the caller, which must match the method's declared return type. A method declared void can still use a bare return; to exit early without producing a value. The compiler enforces that every reachable code path through a non-void method ends in a return (or throws), a static guarantee that catches a large class of "forgot to handle this branch" bugs before the program ever runs.
Official C# site · All languages on AnatomyOf
The interactive tour needs JavaScript. Enable it to hover a callout and trace it into the code.