javac#.netstatic-import

static imports in c#


Does C# has feature like Java's static imports?

so instead of writing code like

FileHelper.ExtractSimpleFileName(file)

I could write

ExtractSimpleFileName(file)

and compiler would know that I mean method from FileHelper.


Solution

  • Starting with C# 6.0, this is possible:

    using static FileHelper;
    
    // in a member
    ExtractSimpleFileName(file)
    

    However, previous versions of C# do not have static imports.

    You can get close with an alias for the type.

    using FH = namespace.FileHelper;
    
    // in a member
    FH.ExtractSimpleFileName(file)
    

    Alternatively, change the static method to an extension method on the type - you would then be able to call it as:

    var value = file.ExtractSimpleFileName();