- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我注意到某些 .NET 结构可以与 null 进行比较。例如:
TimeSpan y = new TimeSpan();
if (y == null)
return;
将编译得很好(与 Guid 结构相同)。
现在我知道结构是值类型并且上面的代码不应该编译,除非有一个重载的运算符 == 接受一个对象。但是,据我所知,没有。
我看过 Reflector 的类(class),也看过 MSDN 上的文档。
他们两个确实实现了以下接口(interface):
IComparable, IComparable<T>, IEquatable<T>
但是,尝试实现相同的接口(interface)似乎没有帮助:
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。
有人知道这是什么原因吗?我只是想获得更好的理解。这不是问题,因为我知道无论如何我都不应该将结构与 null 进行比较。
最佳答案
它是 ==
运算符。
TimeSpan
类重载了相等运算符:
public static bool operator ==(DateTime d1, DateTime d2)
{
return (t1._ticks == t2._ticks);
}
这本身并不能与 null
进行比较,但是...
随着可空类型的到来,每个结构都可以隐式转换为其可空类型,所以当您看到类似的东西时
TimeSpan y = new TimeSpan();
if (y == null)
return;
你没有看到这正在发生:
TimeSpan y = new TimeSpan();
if ((Nullable<TimeSpan>)y == (Nullable<TimeSpan>)null)
return;
Null 获得隐式转换(隐式赋值?),但并非所有 System.Object
对象都这样做:
TimeSpan y = new TimeSpan();
object o = null;
if (y == o) //compiler error
return;
好的,但是相等运算符不接受可为 null 的参数,对吗?
嗯,msdn在这里有帮助,说明:
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.
因此,您可以有效地免费为每个运算符实现可为 null 的实现,并具有固定的定义行为。上面提到的“包含值”是非可空运算符将返回的实际值。
关于c# - 为什么TimeSpan和Guid Structs可以比作null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1225949/
我使用的语言是 C#。 我有以下困境。 DateTime A,DateTime B。如果 A < B,那么我必须计算该时间跨度内每年的天数,并将其乘以对应于该年的系数。我的问题是它可以跨越多年。 例如
我正在尝试将一些 TimeSpans 加在一起进行一些计算。 例如: var timeSpan1 = new TimeSpan.FromMinutes(1); var timeSpan2 = new
我有一个包含 500 个元素的 C# Queue。 我需要通过将 10 个 TimeSpans 分成一组并选择它们的平均值来将它们减少到 50 个元素。 有没有一种干净的方法来做到这一点?我认为 LI
当我尝试执行以下操作(作为示例)时,我收到了 System.FormatException: TimeSpan ts = XmlConvert.ToTimeSpan("P72H"); 我调查了 ISO
当我在R中运行以下代码时,我得到一个奇怪的注释(它仅在我第一次在 session 中运行代码时出现): > library(lubridate) Attaching package: ‘lubrida
我要实现的是获取从日期时间传递到当前日期时间的年份。 DateTime zeroTime = new DateTime(1, 1, 1); DateTime now = DateTime.Now; T
我遵循这里的建议: What is the correct SQL type to store a .Net Timespan with values > 24:00:00? 在名为 Timeshee
这个问题在这里已经有了答案: How do I convert an ISO8601 TimeSpan to a C# TimeSpan? (3 个答案) 关闭 6 年前。 我已经解析 ISO860
下面的代码工作正常: DateTime d1 = DateTime.Now; DateTime d2 = DateTime.Now.AddDays(-1); int d3 = (int)(d1 - d
我使用 POCO 将 RIA 服务与 Entity Framework 相结合。这一切都非常有效(比 LINQ to SQL 好得多)。我遇到的问题是以下代码段: [Query] public IQu
据我所知,我对 MySql Connector 有疑问。我使用 MySql 和 NHibernate。 我有一个包含这个字段的类: private TimeSpan begin; private Ti
我有 TimeSpan 数据表示为 24 小时格式,例如 14:00:00,我想将其转换为 12 小时格式,下午 2:00,我用 google 搜索并在 stackoverflow 和 msdn 中找
Timespan(0,0,secs) 和 Timespan.FromSeconds(secs) 的返回值有区别吗? 在我看来,不同之处在于 FromSeconds 接受 double。 最佳答案 最终
我正在寻找 .net 3.5 中的解决方案,我编写了以下工作解决方案: private string FormatTimeSpan(TimeSpan time) { return String
C# WPF 应用程序 我有一个 SplashScreen通过使用在启动时显示最短时间 Thread.Sleep(int); //int = milliseconds to display splas
我正在研究 Observable.Generate 的使用,以使用 msdn 网站上的示例作为起点来创建按时间间隔采样的结果序列。 以下没有 TimeSpan 选择器的代码不会出现内存泄漏: IObs
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我的 MVC 应用程序中有这个问题。 我有 TimeSpan,其中包含例如 2 天 15 小时 31 分 34 秒。 我只想显示小时/分钟/秒,问题是我想将天数添加到小时,如 63:31:34 。我尝
我在 visual studio 2008 中开发了一个 Web 程序并将其转换为 2010。在 Datagrid 中,我有一个具有 TimeSpan 值的数据列。在 2008 年,我给了它字符串格式
我是一名优秀的程序员,十分优秀!