Using static directives

C# 6.0 NETFx 4.6

Published Updated Author Jeffrey T. Fritz Reading time

using static imports static members so formulas and focused helper APIs read without type-name repetition.

C# 6.0 added using static, which lets you import the static members of a type into the current file. The most familiar examples use System.Math, System.Console, or assertion helpers, but the same idea can also make a small domain helper API feel less ceremonial.

It is a readability feature, not a performance feature. Use it where the source type remains obvious from the surrounding code.

Why it matters

Repeated type qualifiers can drown out the real expression, especially in formulas, small command-line tools, or test code. using static reduces that noise while keeping compile-time checking and IntelliSense intact.

It is most effective when the imported members form a coherent vocabulary rather than a grab bag of unrelated helpers.

Cautions

Do not use using static so broadly that readers lose track of where a member comes from. If a file imports several static types with overlapping member names, clarity drops quickly.

A little goes a long way. Keep it local to files where it genuinely improves the signal-to-noise ratio.

Clean up dense math expressions

`using static` can make formulas and constant-heavy code easier to scan when the containing type is obvious from context.

Valid since C# 6.0

using System;
using static System.Math;

public static class Program
{
    public static void Main()
    {
        double a = 3;
        double b = 4;
        double hypotenuse = Sqrt((a * a) + (b * b));

        Console.WriteLine(hypotenuse);
    }
}

Import a focused helper API

The feature also works with your own static helper classes, which can make validation or DSL-like code read more naturally.

Valid since C# 6.0

using System;
using static Guard;

public static class Program
{
    public static void Main()
    {
        string userName = "Ada";
        int retryCount = 42;

        EnsureHasValue(userName, "userName");
        EnsurePositive(retryCount, "retryCount");
        Console.WriteLine("Validation passed.");
    }
}

public static class Guard
{
    public static void EnsureHasValue(string value, string parameterName)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            throw new ArgumentException("A value is required.", parameterName);
        }
    }

    public static void EnsurePositive(int value, string parameterName)
    {
        if (value <= 0)
        {
            throw new ArgumentOutOfRangeException(parameterName, "The value must be positive.");
        }
    }
}

Learn more

using directive (Microsoft Learn)

Newer capabilities

  1. Global static using

    Introduced in C# 10.0

    C# 10 extends this idea with global using directives, including global using static, so a project can apply the import once instead of repeating it in every file. That broader scope is covered by the Global Using Directives page.