58

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.

Andrej Adamenko
  • 1,650
  • 15
  • 31
IAdapter
  • 62,595
  • 73
  • 179
  • 242
  • Possible duplicate of [Is it possible to reference a method in a static class without referencing the class?](http://stackoverflow.com/questions/30671769/is-it-possible-to-reference-a-method-in-a-static-class-without-referencing-the-c) – Andrej Adamenko Dec 07 '15 at 09:18

5 Answers5

99

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();
Oded
  • 489,969
  • 99
  • 883
  • 1,009
13

No, such feature doesn't exist in C#. You need to specify the class that the static method belongs to unless you are already inside a method of this same class.

In C# though you have extension methods which kind of mimic this.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
8

C# 6.0 under Roslyn Platform supports Static import. It requires statement like:

using static System.Console;

so the code might look like:

using static System.Console;
namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("My test message");
        }
    }
}

The earlier planned version for C# 6.0 had static import without static keyword.

For other new features in C# 6.0 see: New Language Features in C# 6

Habib
  • 219,104
  • 29
  • 407
  • 436
6

Time marches on... it looks like C# might get static imports in the next version, see http://msdn.microsoft.com/en-us/magazine/dn683793.aspx for a preview.

using System;
using System.Console; // using the Console class here

public class Program
{
    public static void Main()
    {
        // Console.WriteLine is called here
        WriteLine("Hello world!");
    }
}

The official documentation for the 'Roslyn' C# compiler lists the feature as 'done'

Michiel van Oosterhout
  • 22,839
  • 15
  • 90
  • 132
  • Jack be Nimble, Jack be Quick … with C# static usings! I cannot express how thrilled I am about this feature. I have been eagerly waiting for this expressiveness to enter the language for years, but never expected I would see the day. – Nicholas Petersen Oct 23 '14 at 20:07
0

If you are using .Net 6+ with C# 10+, you can add all the namepaces, (including the static ones) that you want to use globally in obj > Projectname.GlobalUsings.g.cs and you'll be able to use all the classes that are part of those namepaces without importing. Here are some examples:

 global using global::System.Net.Http.Json;
 global using global::System.Threading;
 global using global::System.Threading.Tasks;
 global using static global::System.Console;
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Auguste
  • 2,007
  • 2
  • 17
  • 25