There is one way to quickly copy a structure into another structure.

public void ReplaceWith( A other )
{
this = other;
}

This will work only for structures. The complete sample follows.

namespace FourGOffice.Learning
{
class Program
{
static void Main(string[] args)
{
var x1 = new A(10, 12);
var x2 = new A(20, 30);
//x2 = x1;
x2.ReplaceWith( x1);
}
}

// You will get a CS1604 if you try to define A as a class.
#if false
class
#else
struct
#endif
A
{
private readonly int _a;
private readonly int _b;
public A( int a, int b)
{
_a = a;
_b = b;
}

public void ReplaceWith( A other )
{
this = other;
}
}
}

 

Advertisements