C# 在数组中存储 class 个实例以用于其他形式

C# Storing class instances in an array to use in other forms

所以我有自己的 class,我想通过单击按钮将 class 的实例存储到 class 数组中,然后以不同的形式使用此数组同一个项目,尽管将​​其声明为 public static,但我仍然无法在我的其他表单上访问它。我什至尝试将其放入 class 本身,看看是否可行。我听说您可以使用列表数组,但我不确定该怎么做以及如何在我的整个项目中访问它。

抱歉,如果我错过了一些明显的东西或者我对 C# 还很陌生的东西。

假设您在 Form1 中创建了对象并希望在 Form2 中使用它。你可以这样做:

// Form1-----------------------------------------------------

public partial class Form1 : Form
{
    public string myName { get; set; }
    public Form1()
    {
        InitializeComponent();
        MyClass myClass = new MyClass() {Name = "John"};
        myName = myClass.Name;
    }
}

public class MyClass
{
    public string Name { get; set; }
}

// Form2-----------------------------------------------------

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        Form1 form1 = new Form1();
        this.label1.Text = form1.myName;
    }
}