为什么将ThrowIfNull实现为:
static void ThrowIfNull<T>(this T argument, string name) where T : class
{
if (argument == null)
{
throw new ArgumentNullException(name);
}
}不是应该重写为:
static void ThrowIfNull<T>(this T argument, string name) where T : class
{
if (object.ReferenceEquals(argument, null))
{
throw new ArgumentNullException(name);
}
}优点:它有助于避免混乱的Equals重载,并可能使代码更清晰。
这有什么不好的吗?应该有一些。
发布于 2011-08-19 17:34:12
这两者之间没有区别。您混淆了重载Equals (在这两个实现中都没有调用)和重载== (因为重载是在编译时执行的,并且编译器对T的了解不足以使用任何特定的重载)。
只是为了说明我的意思:
static void ThrowIfFoo<T>(this T argument, string name) where T : class
{
if (argument == "foo")
{
throw new Exception("You passed in foo!");
}
}使用以下工具测试:
"foo".ThrowIfFoo(); // Throws
string x = "f";
x += "oo"; // Ensure it's actually a different reference
x.ThrowIfFoo(); // Doesn't throwThrowIfFoo不知道T将是一个字符串-因为这取决于调用代码-并且只有在编译ThrowIfFoo时才会执行重载解析。因此,它使用运算符==(object, object)而不是==(string, string)。
换句话说,它是这样的:
object foo1 = "foo";
string tmp = "f";
object foo2 = tmp + "oo";
Console.WriteLine(foo1.Equals(foo2)); // Prints True
Console.WriteLine(foo1 == foo2); // Prints false
Console.WriteLine((string) foo1 == (string) foo2); // Prints True在最后一行中,编译器知道它可以使用==的重载,因为两个操作数都具有编译时类型string。
发布于 2011-08-19 17:32:57
==运算符是在编译时解析的,而不是在运行时解析的,而且由于T是泛型的,编译器将使用object本身提供的==实现,这将检查引用是否相等。
这也正是object.ReferenceEquals所做的:调用object提供的==实现。
发布于 2011-08-19 17:32:04
这主要是一种化妆品。
obj == null将执行引用检查并返回,如果参数为空并且在T中未被覆盖,则Equals也将返回。当一个参数为空时,需要一个相当不可靠/恶意的实现才能返回true。
https://stackoverflow.com/questions/7119594
复制相似问题