c# - TimeSpan 有错误吗?

标签 c# timespan

这将输出“0”:

TimeSpan span = TimeSpan.Zero;
span.Add(TimeSpan.FromMinutes(5));
Console.WriteLine(span.TotalSeconds);   -----> 

但是,这将输出“300”:

TimeSpan span = TimeSpan.Zero.Add(TimeSpan.FromMinutes(5));
Console.WriteLine(span.TotalSeconds);   -----> 

这是一个已知错误吗?

最佳答案

TimeSpan.Add不修改输入 - 它返回一个新的 TimeSpan,它是输入加上加数:

Remarks

The return value must be between TimeSpan.MinValue and TimeSpan.MaxValue; otherwise, an exception is thrown.

The return value is a new TimeSpan; the original TimeSpan is not modified.

关于c# - TimeSpan 有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3732553/

相关文章:

c# - 配置antiforgerytoken 单用mvc asp.net

.net - 您可以舍入 .NET TimeSpan 对象吗?

vb.net - 使用 String.Format 自定义格式时间跨度

c# - 为什么 "1.0"< "-1.0"< "1.1"?

c# - 在通用存储库函数中选择特定列

c# - 检查 C# 数组中 setter 的输入

c# - 有没有一种简洁的方法来实现TimeSpan格式的条件复数化?

c++ - 在没有 UTC 时区的正确时间总和

c# - 将秒转换为 HH :MM:SS with String. 格式

c# - if 语句可以与使用模式匹配的变量赋值相结合吗?