Home > Back-end >  C#: Add the current instance to a list
C#: Add the current instance to a list

Time:04-10

I want to create a list of all instances of a class. For that, I have made a static list of the class inside it and I want to add the instances inside of the constructor, however, I don't know what to reference!
Here is my code:

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
    }
}

Thank you!

CodePudding user response:

Use “this” to reference the current instance.

files.Add(this);
  • Related