25

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?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
thkang
  • 11,215
  • 14
  • 67
  • 83

4 Answers4

46

With C# 6.0 you can.

C# 6.0 allows static import (See using Static Member)

You will be able to do:

using static foo.bar;

and then in your Main method you can do:

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

You can do the same with System.Console like:

using System;
using static System.Console;
namespace SomeTestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Test Message");
            WriteLine("Test Message with Class name");
        }
   }
}

EDIT: Since the release of Visual Studio 2015 CTP, in January 2015, static import feature requires explicit mention of static keyword like:

using static System.Console;
Habib
  • 219,104
  • 29
  • 407
  • 436
8

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?

Community
  • 1
  • 1
mdcuesta
  • 1,739
  • 1
  • 15
  • 17
1

Declare an Action Delegate variable in a suitable scope as follows and use it later:

Action foobar = () => foo.bar.foobar();

or even easier

Action foobar = foo.bar.foobar;

I shall also pay attention to Extension Methods (C# Programming Guide). If you're having methods with parameters, often it's quite cosy to have:

public static class bar
{
    public static void foobar(this string value)
    {
    }
}

and utilize it:

 string s = "some value";
 s.foobar();

This is actually a much better approach.

horgh
  • 17,918
  • 22
  • 68
  • 123
  • Never knew there was an "Action" keyword. however, this means I have to declare `Action ... ` for every function in a static class I want to use... – thkang Dec 28 '12 at 06:11
  • 3
    `Action` isn't a keyword, it's a type. This is a clever response, but _please_ don't actually use it. – Paul Phillips Dec 28 '12 at 06:12
  • 1
    @thkang it's not a keyword. It's a delegate See [Action Delegate](http://msdn.microsoft.com/en-us/library/system.action.aspx) – horgh Dec 28 '12 at 06:13
  • 1
    @PaulPhillips I guess you should explain your comment in greater detail – horgh Dec 28 '12 at 06:14
  • 1
    @KonstantinVasilcov Where they said "I have to declare Action for every function in a static class" gave me a chill. It's rather demanding to have "no dots" and just hiding what is probably a deeper design problem. I imagined opening a method that used this trick on 5-10 different static functions, and I would not want to inherit that code. – Paul Phillips Dec 28 '12 at 06:17
  • 1
    @PaulPhillips I never said "replace everything". While in some circumstances moderate usage of this may improve readibility of the code. – horgh Dec 28 '12 at 06:21
  • 2
    @KonstantinVasilcov No, you didn't, but thkang did. That's what I was responding to. – Paul Phillips Dec 28 '12 at 06:22
  • 1
    @PaulPhillips missed that. Definitely agree with you. – horgh Dec 28 '12 at 06:23
  • 2
    `Action foobar = foo.bar.foobar;` would be slightly lighter weight, not to mention less verbose. – phoog Dec 28 '12 at 06:35
  • 1
    @phoog Thanks! Edited my answer to reflect your comment – horgh Dec 28 '12 at 06:37
0

To add to the answers already here it's important to note that C# is a very typed language. Therefore, unless the method exists in the class you're in, you would never be able to do something like what you're looking for.

However, if you add the using:

using foo;

You can then access it with just the type and method like this:

bar.foo();
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232