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 brings its static members (methods, properties, fields and nested types) into scope so you no longer need to type the class name each time.
Code
csharp
using static System.Convert;
using static System.Math;
class Program
{
static void Main(string[] args)
{
var x = ToInt32(args[0]);
var y = ToInt32(args[1]);
Console.WriteLine(Pow(x, y));
}
}csharp
class Program
{
static void Main(string[] args)
{
var x = Convert.ToInt32(args[0]);
var y = Convert.ToInt32(args[1]);
Console.WriteLine(Math.Pow(x, y));
}
}Notes
- Can aid in readability for classes dealing with just a few concepts, but too many
using staticdirectives will likely become confusing - Extension methods in the imported type are not brought in as plain method calls, they are only available with the usual extension method syntax
- Works with enums too, so
using static System.DayOfWeek;lets you writeMondayinstead ofDayOfWeek.Monday - global using directives C# 10.0 allow
global using staticto apply an import to the whole project