static imports in c#

IAdapter picture IAdapter · Oct 7, 2011 · Viewed 18.2k times · Source

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.

Answer

Oded picture Oded · Oct 7, 2011

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();