[Solved] How do I use the C#6 “Using static” feature?

Introduction

The C#6 “Using static” feature is a great way to simplify your code and make it easier to read. It allows you to access static members of a type without having to specify the type name. This can be especially useful when dealing with large namespaces or when you want to avoid typing out long type names. In this article, we will discuss how to use the “Using static” feature in C#6 and provide some examples of how it can be used.

Solution

The “Using static” feature in C#6 allows you to access static members of a type without having to specify the type name. To use this feature, you must first add a “using static” directive to the top of your code file, specifying the type whose static members you want to access. For example, if you wanted to access the static members of the System.Math type, you would add the following directive to the top of your code file:

using static System.Math;

Once you have added the directive, you can access the static members of the type without having to specify the type name. For example, you can call the Math.Sqrt() method without having to specify the type name:

double result = Sqrt(4);


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.

14

How do I use the C#6 “Using static” Feature?

The C#6 “Using static” feature is a great way to simplify your code and make it easier to read. It allows you to access static members of a type without having to specify the type name. This can be especially useful when dealing with large namespaces or when you want to avoid typing out long type names.

Steps to Use the C#6 “Using static” Feature

  1. Create a new C# project in Visual Studio.
  2. Add a reference to the type you want to use the “Using static” feature with.
  3. Add the “using static” directive to the top of the file.
  4. Use the static members of the type without having to specify the type name.

Example

Let’s say you want to use the static members of the System.Math type. You can do this by adding the following line to the top of your file:

using static System.Math;

Now you can use the static members of the System.Math type without having to specify the type name. For example, you can use the Sqrt() method like this:

double result = Sqrt(4);

This is much simpler than having to type out the full type name each time you want to use a static member.

Conclusion

The C#6 “Using static” feature is a great way to simplify your code and make it easier to read. It allows you to access static members of a type without having to specify the type name. Give it a try and see how it can help you write better code.