Static Methods and Fields
Define members that belong to the type itself.
Static Methods and Fields is a free C# Academy lesson on CoddyKit — lesson 1 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.
Static vs Instance
An instance member belongs to each object; a static member belongs to the class itself. There is exactly one copy of a static member, shared by everyone.
using System;
class Counter
{
public static int Total; // shared
public int Id; // per instance
}
class Program
{
static void Main()
{
Counter.Total = 5;
Console.WriteLine("Shared Total: " + Counter.Total);
}
}Declaring Static Fields
A static field is accessed through the class name, not an instance: ClassName.Field.
using System;
class Bank
{
public static decimal InterestRate = 0.05m;
}
class Program
{
static void Main()
{
Console.WriteLine("Rate: " + Bank.InterestRate);
}
}Static Methods
A static method runs without an instance. You call it on the class. It cannot use instance fields because there is no this.
using System;
class MathUtil
{
public static int Square(int x)
{
return x * x;
}
}
class Program
{
static void Main()
{
Console.WriteLine(MathUtil.Square(7));
}
}Shared State Across Instances
Because a static field has one copy, every instance sees the same value. This is useful for counters and shared configuration.
using System;
class Robot
{
public static int Count;
public Robot()
{
Count++;
}
}
class Program
{
static void Main()
{
new Robot();
new Robot();
new Robot();
Console.WriteLine("Robots created: " + Robot.Count);
}
}Static Methods Cannot Use Instance Data
A static method has no this, so it can only use parameters, locals, and other static members.
using System;
class Calculator
{
public static int Add(int a, int b) => a + b;
public static int Multiply(int a, int b) => a * b;
}
class Program
{
static void Main()
{
Console.WriteLine(Calculator.Add(2, 3));
Console.WriteLine(Calculator.Multiply(4, 5));
}
}Mixing Static and Instance
A class can hold both kinds. Instance methods can read static members, but static methods cannot read instance members.
using System;
class Account
{
public static decimal Fee = 2m;
public decimal Balance;
public decimal NetBalance()
{
return Balance - Fee; // instance method can read static
}
}
class Program
{
static void Main()
{
var a = new Account { Balance = 100m };
Console.WriteLine("Net: " + a.NetBalance());
}
}Static Counters Pattern
Assigning each new object an id from a static counter is a classic use of static fields.
using System;
class Ticket
{
private static int _next = 1;
public int Number;
public Ticket()
{
Number = _next++;
}
}
class Program
{
static void Main()
{
Console.WriteLine(new Ticket().Number);
Console.WriteLine(new Ticket().Number);
Console.WriteLine(new Ticket().Number);
}
}Static Helper Methods
Static methods are great for stateless operations that depend only on their inputs, like formatters and converters.
using System;
class Format
{
public static string Money(decimal amount)
{
return "$" + amount.ToString("0.00");
}
}
class Program
{
static void Main()
{
Console.WriteLine(Format.Money(12.5m));
}
}Calling Static from Static
Static methods can freely call other static methods in the same class without any instance.
using System;
class Geometry
{
public static double CircleArea(double r) => Math.PI * Square(r);
private static double Square(double x) => x * x;
}
class Program
{
static void Main()
{
Console.WriteLine(Geometry.CircleArea(2).ToString("0.00"));
}
}Static Fields Persist
A static field keeps its value for the lifetime of the program, not just one object. Use this for caches or running totals.
using System;
class Logger
{
public static int MessageCount;
public static void Log(string msg)
{
MessageCount++;
Console.WriteLine("[" + MessageCount + "] " + msg);
}
}
class Program
{
static void Main()
{
Logger.Log("start");
Logger.Log("working");
Logger.Log("done");
}
}Putting It Together
Static members model class-wide concepts: shared rates, counters, and stateless helpers, while instance members hold per-object data.
using System;
class Order
{
public static decimal TaxRate = 0.10m;
public decimal Subtotal;
public decimal Total() => Subtotal + Subtotal * TaxRate;
}
class Program
{
static void Main()
{
var o = new Order { Subtotal = 200m };
Console.WriteLine("Total: " + o.Total());
}
}Quick Check
Test your understanding of static members.
Recap
Static members belong to the class and have a single shared copy; instance members belong to each object. Static methods have no this and cannot touch instance data, but instance code can read static members. Use static for counters, shared config, and stateless helpers.
using System;
class Demo
{
public static int Shared;
public int Own;
public static void Bump() => Shared++;
}
class Program
{
static void Main()
{
Demo.Bump();
Demo.Bump();
Console.WriteLine(Demo.Shared);
}
}Frequently asked questions
Is the “Static Methods and Fields” lesson free?
Yes — the full text of “Static Methods and Fields” 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 Methods and Fields”?
Define members that belong to the type itself. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Static Methods and Fields” 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