以下不会编译:测试平等默认值
public void MyMethod<T>(T value)
{
if (value == default(T))
{
// do stuff
}
}
错误:Operator '==' cannot be applied to operands of type 'T' and 'T'
我不能使用value == null
因为T
可能是一个结构。
我不能使用value.Equals(default(T))
,因为value
可能是null
。
什么是测试相等到默认值的正确方法?
回复评论;并且对于信息来说,'EqualityComparer'是BCL使用的标准实现,例如'Dictionary <,>'。 –
2009-12-13 23:28:31
http://stackoverflow.com/questions/65351/null-or-default-comparsion-of-generic-argument-in-c-sharp – nawfal 2013-04-15 10:28:53