我有一个名为Test的静态类和一个私有的List集合.现在我想实现一个getter来将我的List返回给主类.这是正确的方法吗?
其次是可以实现静态构造函数吗?如果没有,我如何正确声明我的列表集合?
static class Storage
{
private static List<string> store;
static Storage()
{
store = new List<string>();
}
//Is it okay to have a getter in my static class to return my List Collection
public static List<string> getList
{
get
{
return store;
}
}
public static void addString(string add)
{
store.Add(add);
}
public static void removeString(string remove)
{
store.Remove(remove);
}
Run Code Online (Sandbox Code Playgroud) 可以放在"view = new Person"按钮事件处理程序中吗?如果我不这样做,并将其放在Form1构造函数中,则只添加我的最后一个值.如果我想声明一个新实例然后将其添加到我的Arraylist中,这是正确的方法吗?
private ArrayList store;
public Form1()
{
InitializeComponent();
store = new ArrayList();
}
private void Form1_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
//Is it okay to declare a new instance of the Person class
// with each button push?
Person view = new Person();
view.firstname = txtFirstName.Text;
view.lastname = txtLastName.Text;
store.Add(view);
txtFirstName.Clear();
txtLastName.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
foreach (Person display in store)
{
MessageBox.Show(display.ToString());
} …Run Code Online (Sandbox Code Playgroud)