C# 全局模块

jac*_*kie 2 c# module

我是 c# 新手,我想知道是否有人可以告诉我如何创建一个全局模块来更改表单,就像在 vb 中所做的那样,然后如何调用该模块。

谢谢

更新:

好吧,所以我有多种形式,而不是一遍又一遍地写相同的两行,它们是..

Form x = new Form1();
x.Show();
Run Code Online (Sandbox Code Playgroud)

是否可以将其添加到类中以使其更容易。我该怎么做?以前从来没有上过课。上课了,但不知道如何编写代码。

She*_*Pro 6

C# 中没有全局模块,您可以使用静态成员创建一个公共类,例如:

例子:

//In a class file 

namespace Global.Functions //Or whatever you call it
{
  public class Numbers
  {
    private Numbers() {} // Private ctor for class with all static methods.

    public static int MyFunction()
    {
      return 22;
    }
  }
}

//Use it in Other class 
using Global.Functions
int Age = Numbers.MyFunction();
Run Code Online (Sandbox Code Playgroud)