Ale*_*yev 13 c# language-features
有人this
在C#方法中有分配的有用示例吗?在求职面试中我被要求过一次,我仍然有兴趣回答自己.
Zer*_*nce 42
当他们说你不能分配给'this'时,其他答案是不正确的.没错,你不能为类类型,但你可以为结构类型:
public struct MyValueType
{
public int Id;
public void Swap(ref MyValueType other)
{
MyValueType temp = this;
this = other;
other = temp;
}
}
Run Code Online (Sandbox Code Playgroud)
在任何时候,结构都可以通过像这样指定'this'来改变自己.