名称空间C#中的方法 [英] Methods inside namespace c#
问题描述
是否可以在不声明c#内部类的情况下调用命名空间内的函数.
Is there any way to call a function that is inside of a namespace without declaring the class inside c#.
例如,如果我有两个完全相同的方法,并且应该在我的所有C#项目中使用,那么有什么方法可以将这些函数放入一个dll并在上面说使用myTwoMethods"顶部并在不声明类的情况下开始使用方法?
For Example, if I had 2 methods that are the exact same and should be used in all of my C# projects, is there any way to just take those functions and make it into a dll and just say 'Using myTwoMethods' on top and start using the methods without declaring the class?
现在,我这样做:MyClass.MyMethod();
Right now, I do: MyClass.MyMethod();
我想做:MyMethod();
I want to do: MyMethod();
谢谢,罗希特(Rohit)
Thanks, Rohit
推荐答案
您不能在类外部声明方法,但是可以在类库项目中使用静态帮助器类来实现.
You can't declare methods outside of a class, but you can do this using a static helper class in a Class Library Project.
public static class HelperClass
{
public static void HelperMethod() {
// ...
}
}
用法(在添加对类库的引用之后).
Usage (after adding a reference to your Class Library).
HelperClass.HelperMethod();
这篇关于名称空间C#中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!