你如何指定哪些派生结构的默认状态应该是在C#中?是在、状态、结构

2023-09-04 12:42:26 作者:元气少女V

我已经看到了SO有关无法使用参数构造函数或不设置字段初始不同的问题,但我觉得那种,我的问题超越了一步。

I've seen different questions on SO about not being able to use parameterless constructors or not setting field initializers, but I think my question kind of goes a step beyond that.

我想知道的是,我将如何去建立一个结构,我需要定义的默认值?说我正在做一个TaxID结构,默认值必须为999-99-9999无论出于何种原因。这是已经完成在.NET中使用其他结构,即日期时间。任何时候你宣布一个日期时间值立即设置为1月1日0001 12:00 AM。为什么它被置为这个值,而不是0/0/0000 00:00:0000 AM?一定有什么事情的背后,使得价值实际上使感觉在它的默认的场面,即使考虑把对我们的限制,通过C#的问候结构。

What I would like to know is, how would I go about setting up the "default" value of a struct I need to define? Say I'm making a TaxID struct, and the default value needs to be 999-99-9999 for whatever reason. This is already done with other structs in .NET, namely the DateTime. Any time you declare a DateTime the value is immediately set to Jan 1 0001 12:00 AM. Why does it get set to this value and not 0/0/0000 00:00:0000 AM? There must be something going on behind the scenes that makes the value actually make "sense" at it's "default", even given the restrictions put on us by c# with regards to structs.

推荐答案

在一般情况下,这应该是可以避免的。如果您需要需要一个良好的,默认情况下,这需要建设,你应该考虑改变的一类。

In general, this should be avoided. If you need to require a good, default case which would require construction, you should consider changing to a class.

其中的核心设计准则结构是:请确保的状态下,所有的实例数据设置为零,false或null(如适用)是有效的。

One of the core design guidelines for structs is: Do ensure that a state where all instance data is set to zero, false, or null (as appropriate) is valid.

有关详细信息,请参见设计准则。

For details, see the design guidelines.

我只是看着在设计准则第2版这一节了,他们在那里详细使用属性和非传统的覆盖来解决这样的一个例子,也是如此。其基本概念是私自保存的方式,0是很好的默认值,并做一些形式在每一个属性和方法的重写(包括的ToString)变换。在他们的情况下,他们用一个正整数,作为一个例子,并一直留有CURVAL-1的私有成员,所以默认值(0)将被视作值1。他们还说有一个int VAL一个构造函数,并保存值-1内部,等等。

I just looked this section up in the design guidelines 2nd edition, and they have an example in detail there using properties and non-conventional overrides to work around this, as well. The basic concept was to save the value privately in a way that 0 is the "good default", and do some form of transform in every property and method override (including ToString). In their case, they used a positive integer as an example, and always save curVal-1 in the private member, so the default value (0) is treated like a value of 1. They added a constructor with an int val, and save value-1 internally, etc.

这似乎是一个很大的隐蔽性,突发的开销,虽然 - 所以我个人还是建议(和使用)的类在这种情况下

This seems like a lot of hidden, unexpected overhead, though - so I'd personally still recommend (and use) a class in this case.

---编辑回应评论---

--- Edit in response to comments ---

日期时间,为你的榜样,是因为0 == 1/1/0001在午夜的方式。日期时间使用一个单一的,ULONG再从1/1/0001 present蜱。这就是为什么(从这里):

DateTime, as your example, is the way it is because 0 == 1/1/0001 at Midnight. DateTime uses a single, ulong to represent ticks from 1/1/0001. That is why (from here):

的日期时间值类型重新presents的日期和时间,其值从午夜12:00:00,1月1日,0001公元(共同的时代)至下午11:59:59不等,12月31日,公元9999 (CE)

"The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) "

这是在蜱全方位​​为ulong。 0,在日期时间的结构是,当您转换为字符串当作1/1/0001 - 值是不是1 + 1 + ...... - 这是一个0

This is the full range of ulong in ticks. A "0" in the struct of DateTime is treated as 1/1/0001 when you convert to a string - the values aren't 1 + 1 + .... - it's a single 0.