6

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
ohSkittle
  • 149
  • 3
  • 14
  • If the method you're calling is static, then you will call it via the class name – Vsevolod Goloviznin Jan 02 '15 at 23:24
  • Yes, the methods are static, is there any way to avoid having to do `myClass.myMethod();` instead of `myMethod();` – ohSkittle Jan 02 '15 at 23:26
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 02 '15 at 23:29

3 Answers3

7

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");
}
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Ok thanks! I was just a bit confused as I have been moving from VB (in which you can do this) – ohSkittle Jan 02 '15 at 23:27
  • 1
    `using System.Console;` produces an error. Correct syntax is `using static System.Console;` as per this post: https://stackoverflow.com/questions/31852389/how-do-i-use-the-c6-using-static-feature/31852390 – Maris B. Apr 30 '19 at 11:21
4

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

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
4

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();
}
Community
  • 1
  • 1
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
  • Use these `using` directives to write something like `using ConditionalAttribute = System.Diagnostics.ConditionalAttribute` when you have to use the original name. It does only work for one specific class, you have to type the class name twice and you literally need to copy-paste-modify the line if you'd like to use multiple classes from a namespace. – ChrisoLosoph Aug 09 '22 at 15:05