Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The using static directive imports the static members and nested types of a specific type directly into the current compilation unit or namespace scope. This allows developers to invoke static methods, properties, fields, events, and constants without qualifying them with their declaring type name.

Syntax

using static <fully-qualified-type-name>;
Unlike a standard using directive, which imports all types within a namespace, using static targets a specific class, struct, interface, or enum.

Mechanics and Scope

When the compiler encounters a using static directive, it modifies the name resolution rules for the current file or namespace block. Included Members:
  • Static Methods, Properties, and Fields: Accessed directly by their identifier.
  • Constants: Because const fields are implicitly static, they are imported.
  • Nested Types: Types declared within the target type become accessible without qualification.
  • Enum Members: When applied to an enumeration, the individual enum values are imported into the local scope.
Excluded Members:
  • Instance Members: The directive strictly ignores instance-level methods, properties, and fields.
  • Extension Methods (Unqualified): Extension methods declared in the target type are brought into scope for extension method invocation (e.g., object.ExtensionMethod()), but they are not imported for unqualified static invocation. To call them as standard static methods, the type name must still be provided.

Code Visualization

using System;
using static System.Math;
using static System.Console;
using static System.DayOfWeek;

namespace DirectiveDemonstration
{
    class Program
    {
        static void Main()
        {
            // 'WriteLine' is resolved from System.Console
            WriteLine("Demonstrating using static");

            // 'PI' and 'Pow' are resolved from System.Math
            double area = PI * Pow(5, 2);

            // 'Monday' is resolved from System.DayOfWeek enum
            DateTime nextMeeting = GetNextDay(Monday); 
        }

        static DateTime GetNextDay(DayOfWeek day) => DateTime.Now;
    }
}

Name Resolution and Ambiguity Rules

The C# compiler enforces strict precedence rules to handle naming collisions when using static is employed:
  1. Local Precedence: Members declared within the current type always take precedence over members imported via using static. If a local method and an imported static method share the same signature, the compiler binds to the local method.
  2. Ambiguous Invocation: If multiple using static directives import members with identical names and signatures from different types, the compiler does not raise an error at the directive declaration. However, if that ambiguous member is invoked, the compiler throws error CS0121 (The call is ambiguous between the following methods or properties). The invocation must then be explicitly qualified with the type name.

Global Modifier (C# 10+)

The using static directive can be combined with the global modifier to apply the static import across all compilation units within the project.
global using static System.Math;
When declared globally, the static members of the specified type are injected into the root scope of the entire assembly, subjecting the entire project to the same name resolution and ambiguity rules described above.
Master C# with Deep Grasping Methodology!Learn More