import a static method
Asked Answered
E

4

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?

Enwind answered 28/12, 2012 at 6:6 Comment(5)
If you write using foo; then bar.foobar() is all you need to invoke it. With a static method, you're always going to have at least one dot.Starchy
is writing bar.foobar() so verbose?Abydos
switch to VB.Net? "Module" in VB does the "trick" - kind of tribute to VB.classic users I guess.Sverdlovsk
Java supports static method imports. Just saying... :)Tekla
You might wanna change the accepted answer - this question has a big enough view count to occasionally be updated.Fragment
H
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?

Horvitz answered 28/12, 2012 at 6:9 Comment(0)
C
47

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;
Crook answered 24/10, 2014 at 14:35 Comment(1)
Note that static import does not work for extension methods at least up to .Net 8. More details here.Ploch
H
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?

Horvitz answered 28/12, 2012 at 6:9 Comment(0)
K
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.

Kirkman answered 28/12, 2012 at 6:9 Comment(10)
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...Enwind
Action isn't a keyword, it's a type. This is a clever response, but please don't actually use it.Starchy
@Enwind it's not a keyword. It's a delegate See Action DelegateKirkman
@PaulPhillips I guess you should explain your comment in greater detailKirkman
@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.Starchy
@PaulPhillips I never said "replace everything". While in some circumstances moderate usage of this may improve readibility of the code.Kirkman
@KonstantinVasilcov No, you didn't, but thkang did. That's what I was responding to.Starchy
@PaulPhillips missed that. Definitely agree with you.Kirkman
Action foobar = foo.bar.foobar; would be slightly lighter weight, not to mention less verbose.Puissance
@Puissance Thanks! Edited my answer to reflect your commentKirkman
S
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();
Scandian answered 28/12, 2012 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.