ohSkittle
ohSkittle

Reputation: 169

Can I do "using namespace.class"?

I was wondering this as I apparently cannot do this. I have added my .dll as as reference and have added using myNamespace; but whenever I want to call one of the functions, I have to use myClass.myMethod(); is there anything I can do so I wont need to reference the class name when calling a procedure? So I would only need myMethod();

Upvotes: 8

Views: 8820

Answers (3)

BradleyDotNET
BradleyDotNET

Reputation: 61379

You cannot do that in any way in current C#. using just puts the namespace into your code so you don't have to explicitly write it each time you need it.

If your class is static and you are using C# 6.0, you can do this:

using static System.Console;

private static void Main(string[] args)
{
    WriteLine("test");
}

Upvotes: 7

t3chb0t
t3chb0t

Reputation: 18754

As the other answers has already explained this won't be possible until C# 6. You can however make your life easier with aliases that allow you to create an custom name for a type. It's very handy for example when some class has a longer name or for whatever other reason:

You define an alias by assigning a type to a name in the using Directive. There are also few other things that you should know about it:

  • The scope of a using directive is limited to the file in which it appears.

  • Create a using alias to make it easier to qualify an identifier to a namespace or type. The right side of a using alias directive must always be a fully-qualified type regardless of the using directives that come before it.

  • Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.

using util = MyNameSpace.MyVeryLongNameStaticUtilityClass;
using other = MyNameSpace.MyVeryLongNameOtherClass;

Then you can use it like it was this type:

private static void Main(string[] args)
{
    var result = util.ParseArgs(args);
    var other = new other();
}

Upvotes: 4

Selman Genç
Selman Genç

Reputation: 101742

You can't for now. But in C# 6.0 you will be able able to use using directives for static classes.

For example:

using System.Console;
// ...
Write(4);

You can see the detailed list for all features here

Upvotes: 4

Related Questions