Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Classes and objects

Reference for C# classes and objects: declaring fields and members, creating instances with new, reference semantics, null, object initializers and access modifiers.

A class is a blueprint: it declares the data (fields, properties) and behavior (methods) that its instances share. An object is one instance of a class, created with new. Classes are reference types: a variable of a class type holds a reference to an object on the managed heap, not the object itself.

Every class derives from System.Object, either directly or through a base class. Members default to private; the class itself defaults to internal. You normally mark what other code may use as public.

C#
public class Counter
{
    private int _value;                 // field
    public int Value => _value;         // property
    public void Increment() => _value++;   // method
}

var c = new Counter();
c.Increment();

Try it

C# Declaring and using a class

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.

C# References, copies and null

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

C#
[attributes] [access] [modifiers] class Name [: BaseClass, IInterface, ...]
{
    // fields, constants, properties, methods, constructors,
    // events, indexers, operators, nested types
}
Part Description
Access public, internal (default for top-level types), or, for nested types, private (default), protected, private protected, protected internal.
Modifiers abstract, sealed, static, partial, unsafe. required and readonly apply to members and structs rather than classes.
Base list At most one base class (listed first) plus any number of interfaces.
Members Everything a class can contain. Each member has its own access level.

Creating objects: new ClassName(args), or the target-typed ClassName x = new(args);. new allocates the object, runs field initializers, then the constructor.

Usage notes

  • Reference semantics: assignment and passing to a method copy the reference. Two variables can point at one object, so a change through one is visible through the other. Use a struct or a record with with when you want copy semantics.
  • Default equality is reference equality. Override Equals and GetHashCode (or use a record) if two separate objects with equal data should count as equal.
  • null is a valid value for every reference variable unless nullable reference types say otherwise. With <Nullable>enable</Nullable> the compiler warns when a non-nullable variable might be null. Calling a member on null throws NullReferenceException.
  • Object initializers (new T { A = 1 }) set accessible members after the constructor ran. Combine with required members to force callers to set them.
  • Fields should usually be private; expose state through properties so you can add validation later.
  • Lifetime is managed: the garbage collector frees an object when nothing refers to it. Resources that need prompt release (files, sockets) implement IDisposable.
  • One class per file is a convention, not a rule. Several top-level types may share a file, and partial lets one class span several.
  • Prefer small classes with one clear responsibility, and sealed for classes not designed for inheritance.