C#:将当前实例添加到列表
C#: Add the current instance to a list
我想创建一个 class 的所有实例的列表。为此,我在其中制作了一个 class 的静态列表,我想在构造函数中添加实例,但是,我不知道要引用什么!
这是我的代码:
class File
{
public static List<File> files = new List<File>();
public string name;
public File(string newName)
{
name = newName;
files.Add(); //This is the line I am having trouble with
}
}
谢谢!
使用“this”引用当前实例。
files.Add(this);
我想创建一个 class 的所有实例的列表。为此,我在其中制作了一个 class 的静态列表,我想在构造函数中添加实例,但是,我不知道要引用什么!
这是我的代码:
class File
{
public static List<File> files = new List<File>();
public string name;
public File(string newName)
{
name = newName;
files.Add(); //This is the line I am having trouble with
}
}
谢谢!
使用“this”引用当前实例。
files.Add(this);