Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the C#6 "Using static" feature?

People also ask

How do I use C on my computer?

It is a bit more cryptic in its style than some other languages, but you get beyond that fairly quickly. C is what is called a compiled language. This means that once you write your C program, you must run it through a C compiler to turn your program into an executable that the computer can run (execute).


It appears the syntax has slightly changed since those blog posts were written. As the error message suggests, add static to your include statement:

using static System.Console;
//      ^
class Program 
{ 
    static void Main() 
    { 
        WriteLine("Hello world!"); 
        WriteLine("Another message"); 
    } 
}

Then, your code will compile.


Note that, in C# 6.0, this will only work for members declared as static.

For example, consider System.Math:

public static class Math {
    public const double PI = 3.1415926535897931;
    public static double Abs(double value);
    // <more stuff>
}

When using static System.Math, you can just use Abs();.
However, you'd still have to prefix PI because it isn't a static member: Math.PI;.

Starting with C# version 7.2, this shouldn't be the case, const values like PI can be used as well.


The static Keyword on a using statement will import only the one, specified type (and it's nested types). Furthermore you must not give the type name anymore. So just add static to your using.

Note: Please use this feature only when the two classes are logically closely related, otherwise it makes reading the code pretty hard.