I\'ve been working with C# for many years now, but just come across this issue that\'s stumping me, and I really don\'t even know how to ask the question, so, to the example!
There's no one-liner for this.
If you do it a lot, you could look into some form of code generation, perhaps using T4 templates and Reflection.
BTW COBOL has a statement for this: MOVE CORRESPONDING HOME TO WORK.
I don't believe there's a language ready solution for this (all the properties would need to have getters and setters).
You could create Address as an abstract class, with a Copy(Address add) method.
Alternatively, you could make Home and Work to HAVE an IAddress, and not extend one. The copy would then be immediate.
You would need to create a method to do this
public void CopyFrom(IAddress source)
{
this.Address1 = source.Address1;
this.Address2 = source.Address2;
this.City = source.city;
}
If you encapsulate the common parts of the home and work address into a separate class it may make your life easier. Then you can simply copy across that property. This seems like better design to me.
Alternatively, you may be able to hack together a solution with reflection and attributes where the value of the properties in one object are copied over to the matching (and marked) properties in the other. Of course this is not going to be a one-line solution either but it may be faster and more maintainable than others if you have a large set of properties.
Jimmy Bogard's AutoMapper is very useful for that kind of mapping operation.
You could build an extension method:
public static void CopyAddress(this IAddress source, IAddress destination)
{
if (source is null) throw new ArgumentNullException("source");
if (destination is null) throw new ArgumentNullException("destination");
//copy members:
destination.Address1 = source.Address1;
//...
}