Skip to main content

Posts

Showing posts with the label Real World Example

What is the Real World use of Copy Constructor in C#

Copy constructor technique is not provided by .NET, but we can write one by our-self, This type of special constructor will take the paramater as object of its type, If you want create an object for a class with existing object values and only if you want to change some values the copy constructor will come for help Example: We have SalesRep class and we want to move all the reptative values in some set of object then we can re-use this,     class SalesRep     {         public int SalesRepID { get ; set ; }         public string SalesRepAddress { get ; set ; }         public string VisitingDoctor { get ; set ; }          public string DoctorAddress { get ; set ; }     } Writing a normal constructor which picks only the doctor details         public SalesRep( string pVisitingDoctor, string pDoctorAddress)         {             VisitingDoctor = pVisitingDoctor;             DoctorAddress = pDoctorAddress;         }