Search code examples
c#static-import

import a static method


How can I import a static method from another c# source file and use it without "dots"?

like:

foo.cs

namespace foo
{
    public static class bar
    {
        public static void foobar()
        {
        }
    }
}

Program.cs

using foo.bar.foobar; // <= can't!

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
             foobar();
        }
    }
}

I can't just foobar();, but if I write using foo; on the top and call foobar() as foo.bar.foobar() it works, despite being much verbose. Any workarounds to this?


Solution

  • This is the accepted answer, but note that as the answer below states, this is now possible in C# 6

    You can't

    static methods needs to be in a class by design..

    Why do static methods need to be wrapped into a class?