Probably the most straightforward way is this:
TextBox New = new TextBox {
Size = Old.Size,
Location = Old.Location,
Multiline = Old.Multiline
};
If this is something you need to do a lot, you could write an extension method that does the same thing:
public static class TextBoxExtensions {
public static TextBox Copy(this TextBox textBoxToCopy) {
var copiedTextBox = new TextBox {
copiedTextBox = textBoxToCopy.Size,
copiedTextBox = textBoxToCopy.Location,
copiedTextBox = textBoxToCopy.Multiline
};
}
}
Usage:
var copyOfOld = Old.Copy();
If you are going to add a lot more properties to copy, I'd think about using AutoMapper and defining a map between TextBox and TextBox. If you're interested in that path, let me know and I'll post a sample.
It would turn this into a one liner, but you'd need a dependency on AutoMapper, but it's available on NuGet: http://nuget.org/packages/AutoMapper/2.2.0
First, take a dependency on AutoMapper.
Define the mapping somewhere in your project:
Mapper.CreateMap<TextBox, TextBox>();
Usage:
var newTextBox = Mapper.Map<TextBox, TextBox>(Old);
or, if you already have an instance you want to stuff it into:
Mapper.Map(Old, newTextBox);
AFAIK, there is no built in, one line solution, so it's either the extension method, or take a dependency on AutoMapper. The extension method does not have to do it that way, you can use reflection or other choices there.
I use AutoMapper in just about all of my projects and it's invaluable.
You can define many mappings in your map definition, then all your copies become one liners. Well, besides the definition :)