.net - C# copy constructor -


i apologize asking basic c# folks. i'm doing coding in c++.

so if want write assignment constructor class, how do that? have far, doesn't seem compile:

public class myclass {     public string s1;     public string s2;     public int v1;      public myclass()     {         s1 = "";         s2 = "";         v1 = 0;     }      public myclass(myclass s)     {         = s;    //error on line     } }   myclass = new myclass(); myclass b = new myclass(a); 

you can't assign class - constructor of form typically copy members of other class:

public myclass(myclass s) {     this.s1 = s.s1;     this.s2 = s.s2;     this.v1 = s.v1; } 

that gives "copy" value of items in other class. if want have reference shared, can't that, wouldn't need constructor - assigning variables works fine that:

var orig = new myclass(); var referencedcopy = orig; // assign reference 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -