什么是"一个例子,这个"分配在C#中?

什么是"一个例子,这个"分配在C#中?

问题描述:

任何人是否有 C#的方法在这里面分配有用的例子吗?我一直在问它在面试一次,我仍然有兴趣在回答自己。

Does anybody have useful example of this assignment inside a C# method? I have been asked for it once during job interview, and I am still interested in answer myself.

其他的答案是不正确的,当他们说你不能分配给'这'。的确,你不能为类类型,但你的可以的一个结构类型:

The other answers are incorrect when they say you cannot assign to 'this'. True, you can't for a class type, but you can for a struct type:

public struct MyValueType
{
    public int Id;
    public void Swap(ref MyValueType other)
    {
        MyValueType temp = this;
        this = other;
        other = temp;
    }
}



在任何时候,一个结构可以通过分配来改变自己'这'像这样。

At any point a struct can alter itself by assigning to 'this' like so.