【发布时间】:2013-02-28 19:04:19
【问题描述】:
我希望可以从任何地方访问如下对象列表,据我所知,这是一个“单例”类。
public class Car
{
public string Name;
public string Color;
public int Value;
}
List<Vehicle> carList = new List<Vehicle>();
Car jeep = new Car();
jeep.Name = "Jeep";
jeep.Color = "Red";
jeep.Value = 20000;
Vehicle.Add(jeep);
这样我就可以在 Windows 窗体应用程序中的任何位置访问和修改它,使用类似按钮单击以下内容:
MessageBox.Show(Vehicle[0].name)
我错过了一些东西。如何将列表车辆公开?
【问题讨论】:
-
1.
List<Car>2.carList[0].name3. 你试过谷歌吗? -
[我想你的答案就在这里。][1] [1]:stackoverflow.com/questions/9468709/using-singleton-in-c-sharp
-
您可能想查看
static类型,但单例模式正是这样做的,所以无论哪种方式。