Skip to content

using static C# 6.0code reduction

Import static members as top-level functions.

There are many common static methods in C# from those on the Math class to Console, Convert and File.

C# 6.0 allows you to declare that you will be using static a type which will bring all the static methods in as top-level method names that no longer require you type the class name each time.

Code

C#
using static System.Convert;
using static System.Math;

class Program
{
    static void Main(string[] args)
    {
        var x = ToInt32(args[1]);
        var y = ToInt32(args[2]);
        Console.WriteLine(Pow(x, y);
    }
}
C#
class Program
{
    static void Main(string[] args)
    {
        var x = Convert.ToInt32(args[1]);
        var y = Convert.ToInt32(args[2]);
        Console.WriteLine(Math.Pow(x, y);
    }
}

Notes

  • Can aid in readability for classes dealing with just a few concepts - too many using static will likely become confusing

More information