导入一个静态方法 [英] import a static method

查看:127
本文介绍了导入一个静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样可以从另一个C#源文件导入一个静态方法,并使用它,而点



这样的:



foo.cs

 命名为foo 
{
公共静态类酒吧
{
公共静态无效foobar的()
{
}
}
}

Program.cs的

 使用foo.bar.foobar; < =不能! 

空间计划
{
类节目
{
静态无效的主要(字串[] args)
{
foobar的( );
}
}
}



我不能只是 foobar的(); ,但如果我写使用富;顶部并调用 foobar的( ) foo.bar.foobar()它的作品,尽管是多详细。这个任何变通办法?


解决方案

这是公认的答案,但要注意的以下国家的答案,这是现在可以在C#6




您不能



静态方法需要在一类设计。



为什么静态方法需要被包装成一个类?


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?

解决方案

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?

这篇关于导入一个静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆