c# - Is there a way to internally set an object equal to a reference? -


say have class called car. normally, if wanted set instance of car equal instance, you'd this:

car car1 = new car(); car car2 = new car();  car1 = car2; 

now if wanted have method did that?

car {  public void setequaltoreference(car reference)  {   = reference;  } } 

and you'd

car1.setequaltoreference(car2); 

and car1 equal car2. know, doesn't work. there way similar?

after reading comment saving , opening serialized version of object want provide answer.

the save method same:

class car {    public void save(string filename) {     // serialize fields of current instance file.   }  } 

to initialize new instance serialized data can use constructor:

class car {    public void car(string filename) {     // initialize new instance serialized data in file.   }  } 

another option provide static factory method:

class car {    public static car open(string filename) {     var car = new car();     // initialize new instance serialized data in file.     return car;   }  } 

the main point "open" method not method on car instance. should either constructor or static method. don't have "internally set object equal reference" (which isn't possible anyway).

in ways line of thought have presented in question similar prototype-based programming. however, c# class-based language opposed prototype-based language.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -