As mentioned earlier, you need to instantiate a new instance of MyResults, and then copy the properties from the Results object. You asked what a โcopy constructorโ is - it is just a constructor that takes an object and uses it to populate the object that is being created. For example:
public class Results { public string SampleProperty1 { get; set; } public string SampleProperty2 { get; set; } } public class MyResults : Results { public MyResults(Results results) { SampleProperty1 = results.SampleProperty1; SampleProperty2 = results.SampleProperty2; } }
The copy constructor is usually more convenient, readable and reusable than when using this code:
MyResults myResults = new MyResults { SampleProperty1 = results.SampleProperty1, SampleProperty2 = results.SampleProperty2 };
If there are many properties and / or you make many changes to the class, you can use reflection (for example, C # Using Reflection to copy properties of a base class ) or a tool like AutoMapper ( http://automapper.codeplex.com ) to copy properties. But often this can be redundant.
Jonathan moffatt
source share