What is an example of “this” assignment in C#?

前端 未结 6 2308
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 01:46

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

6条回答
  •  渐次进展
    2020-12-11 02:14

    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.

提交回复
热议问题