为什么入库时间和将GUID结构进行比较,为空? [英] Why can TimeSpan and Guid Structs be compared to null?
问题描述
我注意到,一些.NET结构可以比作空。 例如:
I've noticed that some .NET structs can be compared to null. For example:
TimeSpan y = new TimeSpan();
if (y == null)
return;
编译就好了(同样具有GUID结构)。
现在我知道,stucts是值类型,而且code以上不被编译,除非有运营商的重载==这需要一个对象。但是,据我所知没有。
我看着学生与反射器,以及在MSDN上的文档。
他们两个都实现以下接口:
will compile just fine (the same with the Guid struct).
Now I know that stucts are value type and that the code above should not compile, unless there's an overload of operator == which takes an object. But, as far as I could tell there isn't.
I've looked at the class with Reflector, and also at the docs on MSDN.
The two of them do implement the following interfaces:
IComparable, IComparable<T>, IEquatable<T>
但是,试图implment相同的接口似乎并没有帮助:
but, trying to implment the same Interfaces did not seem to help:
struct XX : IComparable, IComparable<XX>, IEquatable<XX> {
public int CompareTo(Object obj) {
return 0;
}
public int CompareTo (XX other){
return 0;
}
public bool Equals (XX other){
return false;
}
public override bool Equals(object value){
return false;
}
public static int Compare(XX t1, XX t2){
return 0;
}
}
我使用的是:.NET 2.0的Visual Studio 2005
I'm using: .NET 2.0 Visual Studio 2005.
有没有人有任何想法是这样做的原因是什么? 我只是想获得一个更好的了解。这不是一个问题,因为我知道我不应该比较结构反正空。
Does anyone has any idea what's the reason for this ? I am just trying to get a better understanding. This isn't an issue as I know I shouldn't compare structs to null anyway.
推荐答案
这是 ==
运营商。
在时间跨度
类有相等运算符的重载:
The TimeSpan
class has an overload of the equality operator:
public static bool operator ==(DateTime d1, DateTime d2)
{
return (t1._ticks == t2._ticks);
}
这本身并不使人们有可能用空
来比较,但是
This in itself doesn't make it possible to compare with null
, but...
使用可空类型的到来,每个结构是隐式转换到它的可空类型,所以当你看到类似
With the arrival of nullable types, each struct is implicitly convertible to its nullable type, so when you see something like
TimeSpan y = new TimeSpan();
if (y == null)
return;
您的不看的,这是发生了什么:
You don't see that this is happening:
TimeSpan y = new TimeSpan();
if ((Nullable<TimeSpan>)y == (Nullable<TimeSpan>)null)
return;
空变隐式转换,但是的不是所有的 System.Object的
对象的做(隐式分配?):
Null gets the implicit conversion (implicit assignment?), but not all System.Object
objects do:
TimeSpan y = new TimeSpan();
object o = null;
if (y == o) //compiler error
return;
好了,但平等的运营商并不需要为空的论点,不是吗?
Okay, but the equality operator doesn't take nullable arguments, does it?
好了, MSDN 是帮助在这里,说:
Well, msdn is of help here, stating:
在predefined一元和二元 运营商和用户定义的任何 存在的价值类型的运营商 也可以使用由空类型。 这些运营商产生一个空值 如果[任何]操作数都为空;除此以外, 操作员使用包含的值 来计算结果。
The predefined unary and binary operators and any user-defined operators that exist for value types may also be used by nullable types. These operators produce a null value if [any of] the operands are null; otherwise, the operator uses the contained value to calculate the result.
所以,你得到有效的可为空的实现各运营商免费,有一个固定的定义的行为。而载值上面提到的是实际值非空的操作员将返回。
So you effectively get a nullable implementation for each operator for free, with a fixed defined behaviour. The "contained value" mentioned above is the actual value the non-nullable operator would return.
这篇关于为什么入库时间和将GUID结构进行比较,为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!