c# - TimeSpan 中的冗余方法
问题描述
当我使用TimeSpan
类时,有一个Add()
扩展方法
public TimeSpan Add(TimeSpan ts) {
long result = _ticks + ts._ticks;
// Overflow if signs of operands was identical and result's
// sign was opposite.
// >> 63 gives the sign bit (either 64 1's or 64 0's).
if ((_ticks >> 63 == ts._ticks >> 63) && (_ticks >> 63 != result >> 63))
throw new OverflowException(Environment.GetResourceString("Overflow_TimeSpanTooLong"));
return new TimeSpan(result);
}
基本上它与+
操作员的作用相同。
public static TimeSpan operator +(TimeSpan t1, TimeSpan t2) {
return t1.Add(t2);
}
添加值并在溢出的情况下引发异常。
问题:
为什么.NET
提供这个方法?不会
public static TimeSpan operator +(TimeSpan t1, TimeSpan t2)
足够的?egint
也没有提供Add
方法。
解决方案
推荐阅读
- r - 将存储在 AWS 中的数据转换回 R 中的原始格式
- xamarin - 查看 IsEnabled 属性不适用于 Xamarin 窗体
- kubernetes - 是否有理由自动扩展 kubernetes master?
- react-redux - 用双列表框实现 redux 表单
- php - 如何申请大写和小写的If语句
- ruby - 如何将 Proc 用作块
- python - 为每个异步函数调用创建一个新的记录器,好主意与否?
- node.js - 如何延长普通访问令牌的过期时间
- python - 转储嵌套项的内容
- spring - Bean 名称“cliente”的 BindingResult 和普通目标对象都不能用作请求属性