在任何 Microsoft .NET 类中是否存在零 [0] 常量?

发布于 2024-11-03 09:20:51 字数 225 浏览 1 评论 0原文

我只是很好奇,我知道这没有多大价值,但就是这样……

我想我在某个地方见过类似的东西,但我不确定。

我的意思是这样的:

var zero = Class.Zero;

我尝试查看 Math 类,但它不在那里。

我还知道我可以使用 ushort.Min 这样的无符号值类型来获取零 ( 0 ) 值;这不是我在这里问的...:D

I'm just curious and I know it's not of much value, but here it goes...

I think that I have seen something like that somewhere but I'm not sure.

I mean something like this:

var zero = Class.Zero;

I tried looking at the Math class but it's not there.

I also know that I can use an unsigned value type like ushort.Min to get a Zero ( 0 ) value; it's not what I'm asking here... :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆蝶 2024-11-10 09:20:51

您的意思是 default(T) 吗?

int zero = default(int);

这表示给定类型的默认值,对于 int 来说,这是 0。如果您已经知道您需要零,则不应使用此值,仅当您在运行时有一个需要默认值的类型时。

Do you mean default(T)?

int zero = default(int);

This represents the default value for a given type, for int this is 0. You should not use this if you know already you need zero though, only in the case that you have a type at run time for which you need the default value.

风吹雪碎 2024-11-10 09:20:51

有一个用于 Decimal.Zero 和其他一些更复杂的类型,例如 TimeSpan。零IntPtr。零BigInteger.Zero。但对于常规数字类型,只需使用 0 即可。

There's one for Decimal.Zero and a few other more complex types like TimeSpan.Zero, IntPtr.Zero and BigInteger.Zero. But for regular numeric types, just use 0.

梦萦几度 2024-11-10 09:20:51

.Net Framework 没有为 0 之类的值定义常量。如果您想使用 0 只需使用 0

.Net Framework 中定义的数字常量通常围绕给定数字类型的限制、具有特殊相关性的值或以下情况零值需要特殊/复杂的初始化。例如

  • Int32.Max
  • Int32.Min
  • Double.NaN

对于大多数数字类型来说,文字 0 不适合这些类别(Decimal 是一个例外)

The .Net Framework doesn't define constants for values like 0. If you want to use 0 just use 0

Defined numeric constants in the .Net Framework typically revolve around the limits of a given numeric type, values which hold special relevance or cases where the zero value requires special / complex initialization. For example

  • Int32.Max
  • Int32.Min
  • Double.NaN

The literal 0 doesn't fit these categories for most numeric types (Decimal being one exception)

软糖 2024-11-10 09:20:51

一些不可变引用类型具有“空”值的预定义实例。例如,String 定义了 String.Empty。这样做是因为一个有效的字符串(即使是没有字符的字符串)必须引用一个有效的堆对象,但如果有一千个空字符串变量,它们可能都引用同一个堆对象。除非应用程序根本不使用任何空字符串,否则在应用程序启动时创建一个空字符串实例并允许它在需要空字符串的每个人之间共享将比每次创建一个新的空字符串对象更有效需要一个。

值类型不存在这样的好处。虽然声明了一些值类型常量(例如Math.Pi),但它们的声明是为了方便,而不是效率。说“myDouble = Math.Pi”并不比“MyDouble = 3.14159265358979323846264338327950288419716939937510#”更有效——它只是更容易阅读和验证(如果第一个“328”被错误地输入,任何查看上述代码的人都会注意到) “238”)?如果想要浮点常量零,最自然且最容易阅读的表示法就是 0#。

Some immutable reference types have pre-defined instances for "empty" values. String, for example, defines String.Empty. This is done because a valid String--even one with no characters--must reference a valid heap object, but if there are a thousand empty String variables they may all refer to the same heap object. Unless an application doesn't happen to use any empty strings at all, creating one empty-string instance at application startup and allowing it to be shared among everyone who needs an empty string will be more efficient than creating a new empty string object every time one is needed.

No such benefit would exist with value types. Although there are some value-type constants declared (e.g. Math.Pi), their declaration is for convenience, not efficiency. Saying "myDouble = Math.Pi" is no more efficient than "MyDouble = 3.14159265358979323846264338327950288419716939937510#"--it's just easier to read and validate (would anyone looking at the above code notice if the first "328" were mistyped as "238")? If one wants a floating-point constant zero, the most natural and easiest-to-read notation would simply be 0#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文