实现 IComparable<T> 时,假设 this != null

5
我有一个类型为T的对象,它实现了IComparable<T>。当实现bool Equals (T obj)时,省略检查if (ReferenceEquals(this, null)) { DoSomething() }是否可以?我可以假设由于函数可能会被调用,this已经不为null吗?
非常感谢。

@helium:那并不总是正确的... https://dev59.com/0HA75IYBdhLWcg3wuLjD#3143558 - myermian
2个回答

3

可以假设如果函数已经在一个对象上调用,那么该对象不是null


3

你应该始终假设 this != null,因为 C# 保证它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接