from http://www.parkscomputing.com/dotnet/csharpfaq/oo/default.aspx#faq_004Does C# have copy constructors? No. Objects in C# are always created by reference rather than by value, so a copy constructor is never implicitly called the way that it might be in C++. That being said, nothing prevents a class from implementing a constructor that accepts a reference to an object of the same type. This constructor may be called explicitly. class Abc { public int xyz; public Abc() { xyz = 1; } public Abc(Abc existingObject) { xyz = existingObject.xyz; } }class MainClass { static void Main(string[] args) { Abc firstAbc = new Abc(); firstAbc.xyz = 123; Abc secondAbc = new Abc(firstAbc); System.Console.WriteLine(secondAbc.xyz); } }
http://www.parkscomputing.com/dotnet/csharpfaq/oo/default.aspx#faq_004Does C# have copy constructors?
No. Objects in C# are always created by reference rather than by value, so a copy constructor is never implicitly called the way that it might be in C++. That being said, nothing prevents a class from implementing a constructor that accepts a reference to an object of the same type. This constructor may be called explicitly. class Abc
{
public int xyz; public Abc()
{
xyz = 1;
} public Abc(Abc existingObject)
{
xyz = existingObject.xyz;
}
}class MainClass
{
static void Main(string[] args)
{
Abc firstAbc = new Abc();
firstAbc.xyz = 123; Abc secondAbc = new Abc(firstAbc);
System.Console.WriteLine(secondAbc.xyz);
}
}