C# Reference ยท Keywords and Statements
The class keyword
Reference for the C# class keyword: declare reference types with fields, properties, constructors and methods, plus inheritance, static and partial classes.
class declares a reference type. Variables of a class type hold a reference to an object on the managed heap; copying the variable copies the reference, not the object. Two variables can therefore point to the same instance, and null means "no object".
A class can contain fields, properties, methods, constructors, events, indexers, operators and nested types. It can inherit from exactly one base class (System.Object by default) and implement any number of interfaces. Since C# 12 a class can also declare a primary constructor directly in its header.
public class Counter { public int Value { get; private set; } public void Increment() => Value++; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
[attributes] [modifiers] class Name<T> [(parameters)] [: Base, IFace, ...] [where T : constraint] { // members }
| Modifier | Meaning |
|---|---|
public, internal, file |
Visibility of the type (internal is the default at namespace level; file limits it to one source file). |
abstract |
Cannot be instantiated; may contain abstract members. |
sealed |
Cannot be inherited. |
static |
Only static members; cannot be instantiated or inherited. |
partial |
The declaration is split across several parts or files. |
unsafe |
May use pointer types. |
Nested types default to private accessibility. Members default to private.
Errors: instantiating an abstract or static class is a compile-time error. Accessing a member through a null reference throws NullReferenceException at run time.
Usage notes
- Default constructor: if you write no constructor, the compiler supplies a public parameterless one (protected for abstract classes). Adding any constructor removes it.
- Primary constructors (C# 12): parameters are in scope in the whole class body but are not members. They are captured if used in methods, so assign them to properties if you need them exposed.
- Equality: classes use reference equality unless you override
Equals/GetHashCodeor use arecord class. - Object initializers:
new Account("Ana", 1m) { ... }set accessible properties after construction. - Fields versus properties: keep fields private and expose properties; this keeps validation and future changes possible.
- Finalizers (
~Name()) are rarely needed; preferIDisposablefor cleanup. - Generic classes use type parameters (
class Box<T>).
Related
kw-struct,kw-record,kw-interfacekw-abstract,kw-sealed,kw-static- Tutorials: Classes, Constructors, Inheritance