how to declare global function or method using c#?

后端 未结 4 470
逝去的感伤
逝去的感伤 2021-01-04 11:56

Can anyone tell me how to declare a global function in c#, similar to what a Module does in VB.net? I need to call a function that can be called in my form1, fo

4条回答
  •  猫巷女王i
    2021-01-04 12:34

    You can create a static class (even enclose it in it's own namespace so as not to pollute the main project namespace), then call it from anywhere:

    namespace SomeNamespace
    {
        public static class SomeClass
        {
            public static string SomeMethod() 
            {
                ...
            }
        }
    }
    

    Then, in your code, you can call it using:

    string x = SomeNamespace.SomeClass.SomeMethod();
    

    Or, you can set up a using at the top of the code and just reference it without the namespace:

    using SomeNamespace;
    ...
    string x = SomeClass.SomeMethod();
    

提交回复
热议问题