Static vs Instance Design
Decide when static state is appropriate.
Static vs Instance Design is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Design Decision
Choosing static vs instance is a design choice. The key question: does the behavior depend on per-object state, or only on its inputs?
using System;
class Program
{
// Depends only on inputs -> good static method
static int Max(int a, int b) => a > b ? a : b;
static void Main()
{
Console.WriteLine(Max(3, 8));
}
}Use Static for Stateless Logic
If a method needs no instance fields and just transforms inputs, make it static. Math helpers and formatters are prime examples.
using System;
static class Convert2
{
public static double MilesToKm(double miles) => miles * 1.60934;
}
class Program
{
static void Main()
{
Console.WriteLine(Convert2.MilesToKm(10).ToString("0.0") + " km");
}
}Use Instance for State
If behavior depends on data that varies per object, use an instance. Each object carries its own state.
using System;
class Counter
{
private int _count;
public void Increment() => _count++;
public int Value => _count;
}
class Program
{
static void Main()
{
var a = new Counter();
var b = new Counter();
a.Increment();
a.Increment();
b.Increment();
Console.WriteLine("a=" + a.Value + ", b=" + b.Value);
}
}Shared State Is Global State
A static field is effectively global. It can cause subtle bugs when many parts of a program read and write it. Prefer instance state when each user needs its own copy.
using System;
class BadCart
{
public static int Items; // shared by EVERYONE -> usually wrong for a cart
}
class Program
{
static void Main()
{
BadCart.Items++;
BadCart.Items++;
Console.WriteLine("All carts share: " + BadCart.Items);
}
}Testability
Pure static methods are easy to test: same input, same output. Static mutable state, however, leaks between tests and makes them fragile.
using System;
static class Calc
{
public static int Add(int a, int b) => a + b; // trivially testable
}
class Program
{
static void Main()
{
Console.WriteLine(Calc.Add(2, 2) == 4 ? "pass" : "fail");
}
}Static for Factory Helpers
Static factory methods are a clean way to create configured instances with a descriptive name.
using System;
class Color
{
public int R, G, B;
private Color(int r, int g, int b) { R = r; G = g; B = b; }
public static Color Red() => new Color(255, 0, 0);
public static Color White() => new Color(255, 255, 255);
}
class Program
{
static void Main()
{
var c = Color.Red();
Console.WriteLine(c.R + "," + c.G + "," + c.B);
}
}Avoid Static Just to Skip new
Do not make everything static to avoid creating objects. If a type models a thing with state, instances keep that state isolated and safe.
using System;
class Player
{
public string Name;
public int Health = 100;
public void TakeDamage(int d) => Health -= d;
}
class Program
{
static void Main()
{
var p1 = new Player { Name = "A" };
var p2 = new Player { Name = "B" };
p1.TakeDamage(30);
Console.WriteLine(p1.Name + ": " + p1.Health + ", " + p2.Name + ": " + p2.Health);
}
}Mixing Both Appropriately
Many classes use static members for shared constants or counters and instance members for per-object data. That mix is perfectly idiomatic.
using System;
class Invoice
{
private static int _seq = 1000;
public readonly int Number;
public decimal Amount;
public Invoice(decimal amount)
{
Number = _seq++;
Amount = amount;
}
}
class Program
{
static void Main()
{
var i1 = new Invoice(50m);
var i2 = new Invoice(75m);
Console.WriteLine(i1.Number + " / " + i2.Number);
}
}Concurrency Concern
Shared static state can be accessed by multiple threads at once, risking race conditions. Instance state confined to one thread is safer by default.
using System;
class SafeCalc
{
// No shared mutable state -> naturally thread-safe
public static long Factorial(int n)
{
long result = 1;
for (int i = 2; i <= n; i++) result *= i;
return result;
}
}
class Program
{
static void Main()
{
Console.WriteLine(SafeCalc.Factorial(5));
}
}A Simple Rule of Thumb
Static = behavior that belongs to the type and uses no per-object state. Instance = behavior that reads or changes a specific object. When in doubt, prefer instance for anything stateful.
using System;
class Temperature
{
public double Celsius; // instance state
public static double ToF(double c) => c * 9 / 5 + 32; // stateless helper
}
class Program
{
static void Main()
{
var t = new Temperature { Celsius = 30 };
Console.WriteLine(Temperature.ToF(t.Celsius));
}
}Putting It Together
Good design separates stateless helpers (static) from stateful models (instance), keeping shared mutable state to a deliberate minimum.
using System;
static class TaxRules
{
public static decimal Apply(decimal amount, decimal rate) => amount + amount * rate;
}
class Sale
{
public decimal Subtotal;
public decimal Total(decimal rate) => TaxRules.Apply(Subtotal, rate);
}
class Program
{
static void Main()
{
var s = new Sale { Subtotal = 100m };
Console.WriteLine(s.Total(0.2m));
}
}Quick Check
Test your understanding of static vs instance design.
Recap
Choose static for stateless logic that depends only on inputs (helpers, factories, constants) and instance for behavior tied to per-object state. Static mutable state is effectively global, harder to test, and risks concurrency bugs, so keep it minimal and deliberate.
using System;
static class Util { public static int Sq(int n) => n * n; }
class Box { public int Size; public int Area() => Util.Sq(Size); }
class Program
{
static void Main()
{
var box = new Box { Size = 4 };
Console.WriteLine(box.Area());
}
}Frequently asked questions
Is the “Static vs Instance Design” lesson free?
Yes — the full text of “Static vs Instance Design” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Static vs Instance Design”?
Decide when static state is appropriate. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Static vs Instance Design” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Static Methods and Fields
- Static Classes for Utilities
- Constants and readonly Fields
- Static vs Instance Design