It replaces the value, basically. It effectively copies all the fields from the right side to the left… except it works even if the fields are readonly. And yes, it does look distinctly weird, and it’s somewhat scary.
Example:
using System;
class Test
{
static void Main()
{
Point point = new Point(10, 20);
point.ReplaceWith(new Point(2, 3));
Console.WriteLine(point); // (2, 3)
}
}
struct Point
{
private readonly int x;
private readonly int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public void ReplaceWith(Point other)
{
this = other;
}
public override string ToString()
{
return string.Format("({0}, {1})", x, y);
}
}
For more information, read section 7.6.7 of the C# 4 spec, which includes:
[text about its use in a struct constructor]
When
this
is used in a primary-expression within an instance method or instance accessor of a struct, it is classified as a variable. The type of the variable is the instance type of the struct within which the usage occurs.
If the method or accessor is not an iterator, the
this
variable represents the struct for which the method or accessor was invoked, and behaves exactly the same as aref
parameter of the struct type.[text about iterators]