C# Academy · Lesson

Static Classes for Utilities

Group helper functions in static classes.

Lesson 2 of 413 steps

Static Classes for Utilities is a free C# Academy lesson on CoddyKit. This is lesson 2 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, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.

What Is a Static Class?

A static class is declared with static class. It can contain only static members and cannot be instantiated. It is the natural home for utility functions.

using System;

static class StringUtils
{
    public static string Shout(string s) => s.ToUpper() + "!";
}

class Program
{
    static void Main()
    {
        Console.WriteLine(StringUtils.Shout("hello"));
    }
}

Cannot Create Instances

You never write new for a static class. All access goes through the class name directly.

using System;

static class MathUtils
{
    public static int Cube(int x) => x * x * x;
}

class Program
{
    static void Main()
    {
        // No instance needed
        Console.WriteLine(MathUtils.Cube(3));
    }
}

Only Static Members Allowed

The compiler enforces that every member of a static class is static. Instance fields or constructors are not permitted.

using System;

static class Temperature
{
    public static double ToFahrenheit(double c) => c * 9 / 5 + 32;
    public static double ToCelsius(double f) => (f - 32) * 5 / 9;
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Temperature.ToFahrenheit(100));
        Console.WriteLine(Temperature.ToCelsius(32));
    }
}

Grouping Related Helpers

A static class groups functions that share a theme, giving them a clear namespace like Validation.IsEmail(...).

using System;

static class Validation
{
    public static bool IsPositive(int n) => n > 0;
    public static bool IsEmail(string s) => s.Contains("@") && s.Contains(".");
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Validation.IsPositive(5));
        Console.WriteLine(Validation.IsEmail("a@b.com"));
    }
}

The Built-in Math Class

The .NET Math class is a static class. You never instantiate it; you just call Math.Sqrt, Math.Max, and so on.

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(Math.Max(3, 9));
        Console.WriteLine(Math.Sqrt(144));
        Console.WriteLine(Math.Round(3.14159, 2));
    }
}

Static Class with Static Fields

A static class can hold static fields for shared constants or configuration used by its methods.

using System;

static class Pricing
{
    public static decimal Tax = 0.08m;

    public static decimal WithTax(decimal price) => price + price * Tax;
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Pricing.WithTax(100m));
    }
}

Pure Functions

Utility methods are best as pure functions: output depends only on inputs, with no hidden state. This makes them predictable and testable.

using System;

static class Geometry
{
    public static double RectArea(double w, double h) => w * h;
    public static double TriArea(double b, double h) => 0.5 * b * h;
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Geometry.RectArea(4, 5));
        Console.WriteLine(Geometry.TriArea(6, 3));
    }
}

Composing Utility Methods

Static helpers can call each other, building larger operations from smaller ones.

using System;

static class TextUtils
{
    public static string Clean(string s) => s.Trim().ToLower();
    public static string Slug(string s) => Clean(s).Replace(" ", "-");
}

class Program
{
    static void Main()
    {
        Console.WriteLine(TextUtils.Slug("  Hello World  "));
    }
}

Static Class vs Static Members in Normal Class

If a type is only helpers, make the whole class static. If it mixes helpers with instances, keep it a normal class with some static members.

using System;

static class Convert2
{
    public static int FeetToInches(int feet) => feet * 12;
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Convert2.FeetToInches(3) + " inches");
    }
}

Keeping Utilities Discoverable

Group helpers into well-named static classes (DateUtils, StringUtils) so other developers can find them easily.

using System;

static class DateUtils
{
    public static bool IsWeekend(DayOfWeek d) =>
        d == DayOfWeek.Saturday || d == DayOfWeek.Sunday;
}

class Program
{
    static void Main()
    {
        Console.WriteLine(DateUtils.IsWeekend(DayOfWeek.Sunday));
        Console.WriteLine(DateUtils.IsWeekend(DayOfWeek.Monday));
    }
}

Putting It Together

Static utility classes centralize stateless logic behind a clear name, reducing duplication across your codebase.

using System;

static class MoneyUtils
{
    public static string Format(decimal amount) => "$" + amount.ToString("0.00");
    public static decimal ApplyDiscount(decimal price, double pct) =>
        price * (decimal)(1 - pct / 100);
}

class Program
{
    static void Main()
    {
        decimal discounted = MoneyUtils.ApplyDiscount(80m, 25);
        Console.WriteLine(MoneyUtils.Format(discounted));
    }
}

Quick Check

Test your understanding of static classes.

Recap

A static class (static class X) cannot be instantiated and may contain only static members. It is the standard home for stateless utility functions and constants, grouped under a descriptive name. The built-in Math class is a familiar example.

using System;

static class Util
{
    public static int Double(int n) => n * 2;
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Util.Double(21));
    }
}
Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “Static Classes for Utilities” lesson free?

Yes — the full text of “Static Classes for Utilities” is free to read here on the web. 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. The C# Academy course includes 4 lessons in total.

What will I learn in “Static Classes for Utilities”?

Group helper functions in static classes. 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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Static Classes for Utilities” 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

  1. Static Methods and Fields
  2. Static Classes for Utilities
  3. Constants and readonly Fields
  4. Static vs Instance Design
← Back to C# Academy