0

Simple question that I can't seem to wrap my head around. Say I want to save time/space when working with commonly used System classes and I want to assign an entire class/subclass such as System.Threading.Thread to a variable so that I can use the shortened Variable.ThreadMethod() whenever I want to use a method of the Thread class.

I though it was done with the following:

using test = System.Threading.Thread;

However this throws the "Invalid token 'using' in class, struct or interface declaration." The context of what I am trying to do is the following:

    using test = System.Threading.Thread;
    public void Method()
    {
       test.Sleep(1000); //Same as System.Threading.Thread.Sleep(1000);
    }
4
  • 5
    are you mixing using directive with using statement Commented Mar 20, 2014 at 14:53
  • 1
    @Habib: No, he isn't. Commented Mar 20, 2014 at 14:55
  • BTW, that isn't a variable. Commented Mar 20, 2014 at 14:55
  • 1
    BTW, the usual (idiomatic) way of doing this is to have using System.Threading; and then just Thread.Sleep(1000);, or to spell it out in full if necessary. Using using like this is rare, and I wouldn't recommend it (characters are cheap, confusion is not). Commented Mar 20, 2014 at 15:00

3 Answers 3

4

using directives can only appear at the top of the file or namespace.

Sign up to request clarification or add additional context in comments.

Comments

2

You have to put the using on top of your c# file before declaring a class.

This works:

using test = System.Threading.Thread;

namespace Y
{
    public class X
    {
        public void Method()
        {
           test.Sleep(1000); //Same as System.Threading.Thread.Sleep(1000);
        }
    }
}

Comments

2

Put your using with the others using:

using test = System.Threading.Thread;

namespace MyNamespace
{
    class MyClass
    {
        test.Sleep(10);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.