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

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.


Comments

  1. Turner

    • 2015/2/16

    C is probably the most widely known programming language. It is used as the reference language for computer science courses all over the world, 

  2. Damien

    • 2016/7/27

    No. C++ is a superset of C; (almost) anything you can do in C, you can do in C++. If you already know C, you will easily adapt to the object-oriented features of C++. If you don't know C, you will have to learn the syntax of C-style languages while learning C++, but you shouldn't have any conceptual difficulties.

  3. Gashi

    • 2019/2/28

    Variable Declaration: The next part of any C program is the variable declaration. It refers to the variables that are to be used in the function 

  4. Stanley

    • 2018/1/23

    We tend to use letter c before a, o and u (back vowels) and before r and l, but k before e, i and y. There are some common exceptions e.g. the name Kate, the word ‘skate’ which comes from Scandinavia, and some of Australia’s fauna e.g. kangaroo, koala, kookaburra.

  5. Phillip

    • 2020/3/17

    C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of 

  6. Ali

    • 2017/8/3

    Fill in the blank – You can provide students with words and pictures that are missing the letter for /k/. Students will then fill in the missing letter based on the spelling rule. Write the room – Students can look for objects around the room and determine whether those objects use the <c, k, ck> for the /k/ sound.

  7. Rowen

    • 2017/7/5

    It has become one of the most widely used programming languages, with C compilers from various vendors available for the majority of existing computer 

  8. Damari

    • 2019/4/23

    After the installer is installed, you can use it to customize your installation by selecting the workloads, or feature sets, that you want. Here's how. Find the workload you want in the Installing Visual Studio screen. For core C and C++ support, choose the "Desktop development with C++" workload.

  9. Kellan

    • 2019/3/25

    It is a structured programming language that is machine-independent and extensively used to write various applications, Operating Systems like 

  10. Permeti

    • 2018/6/3

    Before you start doing programming using C programming language, you need the have Xcode setup, you will be able to use GNU compiler for C/C++.

  11. Prifti

    • 2021/4/19

    Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one 

  12. Kristopher

    • 2021/4/20

    learn-c.org is a free interactive C tutorial for people who want to learn C, fast.

Comments are closed.

Recent Posts