c# - Replace list value by reference -


i have class looks this:

public class organization {     public list<ijob> jobs { get; set; }     public ijob bigboss { get; set; }      public organization()     {         bigboss = new doctor();          jobs = new list<ijob>         {             bigboss,             new doctor(),             new doctor()         };     } } 

if set bigboss property new value, value updated in list well. after bigboss pointing new, first item in list should well. know following code won't in c# because of way c# works references:

    static void main(string[] args)     {         var test = new organization();          test.bigboss = new developer();         //test.jobs[0] still pointing doctor, not developer         console.writeline(test.jobs[0]);     } 

is there other clean way of doing this?

jobs[0] reference bigboss = new doctor(); when call constructor new organization();

when change: test.bigboss = new developer(); bigboss refer developer jobs[0] still refer old bigboss.

you can change organization this:

public class organization {     public list<ijob> jobs { get; set; }     private ijob bigboss;     public ijob bigboss { {return jobs[0];} set { jobs[0] = value; } }      public organization()     {                bigboss = new doctor();              jobs = new list<ijob>         {             bigboss,             new doctor(),             new doctor()         };               } } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -