Skip to content
Codewilds

Lesson 21 of 34 · Classes & Objects

C# static Members, const and Static Classes

medium20 XP3 min read

Every hero object has its own name and health. But some data belongs to the class as a whole: how many monsters have been spawned, the maximum level in the game, a conversion rate. And some methods do not need an object at all — Math.Max(3, 9) works without creating a Math. That is what static is for.

Instance members vs static members

public class Monster
{
    public static int Spawned;      // one value shared by the whole class
    public string Name;             // one value per object

    public Monster(string name)
    {
        Name = name;
        Spawned++;                  // instance code can use static members
    }
}

new Monster("Goblin");
new Monster("Troll");
Console.WriteLine(Monster.Spawned);   // 2 — accessed through the class name

Static members are used through the class name (Monster.Spawned), never through an object. Static methods cannot touch instance fields, because there is no particular object for them to read — and there is no this inside them.

Static methods and factory methods

Methods that only work with their parameters are naturally static. A common pattern is a static factory method: a named alternative to a constructor.

public class Monster
{
    public string Name { get; }
    public int Level { get; }

    public Monster(string name, int level)
    {
        Name = name;
        Level = level;
    }

    public static Monster Rat() => new Monster("Rat", 1);
    public static Monster Dragon(string name) => new Monster(name, 40);
}

var boss = Monster.Dragon("Ignis");

const and static readonly

const declares a compile-time constant. It is implicitly static:

public class Game
{
    public const int MaxLevel = 99;
    public const string Title = "Sharpforge";
    public static readonly DateOnly Launch = new DateOnly(2026, 1, 1);
}

Console.WriteLine(Game.MaxLevel);

const only works for simple values known at compile time (numbers, bool, char, strings). For anything created at runtime, use static readonly: it is set once and never changes afterwards.

Static classes

A class marked static can only contain static members and can never be instantiated. It is a home for helper methods — Math, Console and File are static classes:

public static class Distance
{
    public const double MetersPerFoot = 0.3048;

    public static double FeetToMeters(double feet) => feet * MetersPerFoot;
}

Console.WriteLine(Distance.FeetToMeters(10));   // 3.048

This is also why your Main method and the methods in Program have been static: they run without any object being created first.

Shared state needs care

A static field is shared by all code in the program. That makes it handy for counters and caches, but easy to misuse: if two parts of a program change the same static value, they affect each other. Prefer instance data, and keep mutable static state small and obvious.

Common mistakes

  • Using an instance member from a static method: static void Heal() { Health += 10; } fails with CS0120.
  • Calling a static member through an object: goblin.Spawned does not compile; use Monster.Spawned.
  • const for runtime values: const DateTime Now = DateTime.Now; does not compile — use static readonly.

Assignment

  1. In Monster:
    • the constructor stores Name and clamps Level to 1–MaxLevel (MaxLevel is a const 50), and increases the static Count;
    • ResetCount() sets Count back to 0;
    • the static factory Boss(name) returns a monster with level MaxLevel.
  2. In the static class Gold (1 gold = 100 silver = 10,000 copper):
    • ToCopper(gold, silver, copper) returns the total in copper: ToCopper(1, 2, 3) is 10203;
    • Format(copper) returns "1g 2s 3c" for 10203.

Premium lesson

Unlock the full Learn C# course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Static members in other languages

Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.

Lesson last updated on .

Write code ↓
C#+20 XP

Lesson 21 is part of the full course. The first 8 lessons are free.

Premium lesson

Unlock the full Learn C# course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.