8

Is there a way to "Import" a static class in C# such as System.Math?

I have included a comparison.

Imports System.Math

Module Module1

    Sub Main()
        Dim x As Double = Cos(3.14) ''This works
    End Sub

End Module

Vs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Math; //Cannot import a class like a namespace

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            double x = Math.Cos(3.14);
            double y = Cos(3.14); //Cos does not exist in current context
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    There isn't a way to do that in C#. I also [would like to have something like it](http://codecrafter.blogspot.com/2009/09/c-static-classes-are-not-real-classes.html). – Jordão Oct 09 '10 at 02:17
  • As of C# 6, [the answer is now YES](https://stackoverflow.com/a/34777014/199364). – ToolmakerSteve Dec 23 '17 at 14:22

5 Answers5

10

UPDATE: As of C# 6, the answer is now YES.


No, in C# you can only import namespaces, not classes.

However, you can give it a shorter alias:

using M = System.Math;

Now you can use the alias instead of the class name:

double y = M.Cos(3.14);

Be careful how you use it, though. Most of the time the code is more readable with a descriptive name like Math rather than a cryptic M.


Another use for this is to import a single class from a namespace, for example to avoid conflicts between class names:

using StringBuilder = System.Text.StringBuilder;

Now only the StringBuilder class from the System.Text namespace is directly available.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
9

An updated answer to this question is YES as of C# 6.0, which provides a using static feature. So, for instance, using static System.Math; allows the static members of System.Math to be accessed without future qualification of the Math class.

Related SO answers:
Can Math references be shortened in C#?
How do I use the C#6 “Using static” feature?

External References:
GitHub - New Language Features in C# 6
Intellitect - Static Using Statement in C# 6.0

Mike Lowery
  • 2,630
  • 4
  • 34
  • 44
u8it
  • 3,956
  • 1
  • 20
  • 33
5

There isn't. You need to explicitly invoke methods as features of classes in C#.

CesarGon
  • 15,099
  • 6
  • 57
  • 85
2

I was thinking maybe some form of extension methods? This could be tweaked of course.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double x = Math.Cos(3.14);
            double y = 3.14;
            Console.WriteLine(y.Cos());
        }
    }

    public static class Extension
    {
        public static double Cos(this double d)
        {
            return Math.Cos(d);
        }
    }
}
CesarGon
  • 15,099
  • 6
  • 57
  • 85
0

Starting with C# version 6, static classes can be imported by the following syntax.

using static System.Console;
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
Nantharupan
  • 594
  • 3
  • 15