Bud*_*Joe 7 .net c# stack-overflow sorting lambda
在什么情况下,这个代码会出现System.StackOverflowException错误?
Accounts.Sort((x, y) => string.Compare(x.AccountId, y.AccountId));
Run Code Online (Sandbox Code Playgroud)
更新:
该属性写为:
public string AccountId
{
get { return _accountId; }
set { _accountId = value; }
}
Run Code Online (Sandbox Code Playgroud)
没什么特别的.排序也不会被覆盖.
所以我遇到了一个棘手的情况,我在比较方法中得到了StackOverflow异常.
我的比较方法:
public bool Equals(Type rhs)
{
if (rhs == null) return false;
if (this == rhs) return true;
return this.randomField.Equals(rhs.randomField);
}
Run Code Online (Sandbox Code Playgroud)
我的运营商:
public static bool operator ==(Type lhs, Type rhs)
{
if (lhs == null)
return (rhs == null);
else
return lhs.Equals(rhs);
}
Run Code Online (Sandbox Code Playgroud)
所以,发生的是==运算符调用Equals方法,然后在运行该行时调用==运算符this == rhs.解决方案是将线路转换为Object.ReferenceEquals(this, rhs).
| 归档时间: |
|
| 查看次数: |
1888 次 |
| 最近记录: |