c# - How to return object from list in constructor -


i have retrieved static table database in list. if calls constructor of class id, constructor finds object in list , copies 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 create new instance of object. possible return object list directly? this:

public class myclass {     public int id { get; set; }     public string text { get; set; }      public myclass(int instanzid)     {         = cachedlist().find(t => t.id == instanzid);     } } 

i know it's easy in static method, how can done in constructor?

well no, not via constructor. can via method. example factory:

public myclass findorcreate(int instanceid) {     myclass obj = cachedlist().find(t => t.id == instanzid);     //create obj when not exist      return obj; } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -