I have retrieved a static table from a database in a list. If someone calls the constructor of that class with an ID, the constructor finds the object in the list and copies all values.
public class MyClass
{
public int Id { get; set; }
public string Text { get; set; }
public MyClass(int instanzId)
{
MyClass myClass = CachedList().Find(T => T.Id == instanzId);
Id = myClass.Id;
Text = myClass.Text;
}
}
This will always create a new instance of that object. Is it possible to return the object from the list directly? Like this:
public class MyClass
{
public int Id { get; set; }
public string Text { get; set; }
public MyClass(int instanzId)
{
this = CachedList().Find(T => T.Id == instanzId);
}
}
I know it's easy in a static method, but how can that be done in the constructor?
new MyClass()?