本文介绍了Object.ReferenceEquals从不打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
?谁能告诉我,为什么下面的条件不打
列表<&日期时间GT;时间戳=新的List<&日期时间GT;();
timestamps.Add(DateTime.Parse(2011/8/5上午04点34分43秒));
timestamps.Add(DateTime.Parse(2011/8/5上午四点35分43秒));
的foreach(日期时间中X时间戳)
{
如果(Object.ReferenceEquals(X,timestamps.First()))
{
//不要攻击
Console.WriteLine(你好);
}
}
解决方案
由于日期时间是值类型的,一成不变的,因此引用将不等于即使值。
您意思是做这样的事情?值比较:
如果(DateTime.Compare(X,timestamps.First())== 0)
{
//不要攻击
Console.WriteLine(你好);
}
Can anyone tell me why the following condition does not hit?
List<DateTime> timestamps = new List<DateTime>();
timestamps.Add(DateTime.Parse("8/5/2011 4:34:43 AM"));
timestamps.Add(DateTime.Parse("8/5/2011 4:35:43 AM"));
foreach(DateTime x in timestamps)
{
if (Object.ReferenceEquals(x, timestamps.First()))
{
// Never hit
Console.WriteLine("hello");
}
}
解决方案
Because DateTime is a value type, immutable and so the references will not be equal even though the values are.
Are you meaning to do something like this? Value comparison:
if (DateTime.Compare(x, timestamps.First()) == 0)
{
// Never hit
Console.WriteLine("hello");
}
这篇关于Object.ReferenceEquals从不打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!