Valid tive C # Item 8: ensure that 0 is a valid state for value types

Source: Internet
Author: User

Valid tive C # Item 8: ensure that 0 is a valid state for value types

In. net, all object information is set to 0 by default during initialization. For value types, we cannot avoid setting the value to 0 when creating a new value type instance. This is the default value of the type.

The only special case is enumeration (Enum ). We should not create an enumeration that does not contain 0 as its value. All enumerations are inherited from system. valuetype. The enumerated value starts from 0, but we can modify it: (p.s. Although Pluto is no longer a member of the Solar System, bless)

Public Enum planet
{
Mercury = 1,
Venus = 2,
Earth = 3,
Mars = 4,
Jupiter = 5,
Saturn = 6,
Neptune = 7,
Uranus = 8,
Pluto = 9
}

Planet sphere = new planet ();

Now the value of sphere is 0, which is not a valid value. Although no error is reported, 0 does not mean anything. When creating a reference, make sure that 0 is an enumeration value. If you declare an enumeration as a binary type, you need to define 0 to represent none.

If this continues, we can only force users to assign values explicitly during initialization.

Planet sphere = planet. Mars;

This makes it difficult for us to use this type in other types, because we don't know what value to initialize it.

Using this type to create an object will create an invalid planet.

Observationdata d = new observationdata ();

Make sure that 0 is a valid value. If possible, it should be the best default value. There is no optimal solution for planet enumeration. When the user does not use it, it should not contain any meaning. We can use 0 to modify our enumeration to make it more reasonable:

Public Enum planet
{
None = 0,
Mercury = 1,
Venus = 2,
Earth = 3,
Mars = 4,
Jupiter = 5,
Saturn = 6,
Neptune = 7,
Uranus = 8,
Pluto = 9
}

Planet sphere = new planet ();

The enumerated value of sphere is none, indicating that no user data exists. The effect of adding the default value on observationdata is that the enumerated value of the newly initialized object is 0, which indicates none. Then you can modify it in the observationdata constructor. Note that the default constructor is still valid. Users can construct a default object, so we cannot prevent them from doing so.

Before we end the discussion of Enum, we also need to talk about some special cases of enumeration when using flag. When using the flags attribute, we should set the enumerated value representing the meaning of none to 0: (P, S. for more information about flags enumeration, see the application of the flagsattribute attribute in Enum)

[Flags]
Public Enum styles
{
None = 0,
Flat = 1,
Sunken = 2,
Raised = 4
}

Many developers use flags for enumeration through the binary operator. Unreasonable 0 values can cause serious problems. If the flag is 0, the conditions in the following example will never be true:

If (flag & styles. Flat )! = 0)
Dosomething (); // it will never be executed if the flag is 0

Therefore, if flags is used, we should make sure that 0 represents none.

Another common initialization problem occurs in the reference type included in the value type. String is a common example (in p.s.. net, string is a reference type, although some operators are overloaded to give users the illusion of value type ).

Public struct logmessage
{
Private int _ errlevel;
Private string _ MSG;
}

Logmessage mymessage = new logmessage ();

Mymessage contains a null reference type. This will not cause any problems. However, when you use an attribute to obtain its value, problems may occur. Therefore, we need to add it. If it is null, the logic of string. Empty is returned. In our structure, we should check whether the attribute is null.

Public struct logmessage
{
Private int _ errlevel;
Private string _ MSG;

Public String message
{
Get
{
Return (_ MSG! = NULL )? _ MSG: String. empty;
}
Set
{
_ MSG = value;
}
}
}

The value of all types during system initialization is 0, which is unavoidable. If possible, set 0 to the original default value. In special cases, for example, flags enumeration, it should be set to none.

Translated from Objective C #: 50 specific ways to improve your C # by Bill Wagner

Back to directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.