C #, the initial value of Enum is 0

Source: Internet
Author: User

When the newly written Windows Service transmits data to the Web end, the data is empty or the service exception occurs.

Check the cause carefully. The original data passed contains an enumerated field. By default, the basic type of each element in the enumeration is
Int. In the database, int is used for storage. When I insert data of this enumeration type into the database table, the corresponding values cannot be found in the enum class. In the database, if this column can be empty, you may also forget to insert the value.
If the int type is null, 0 is assigned by default when it is read from the database. In my Enum definition, it is written in this way.

Enum site {[enummember] domicile = 1, [enummember] Hotel = 2, [enummember] gasstation = 3 ,}

Okay, this is the case now.

Int sitefromdatabase = 0; site = (SITE) sitefromdatabase;

If the site value is 0, it is impossible for Windows service to pass such data to the front-end. It is reasonable to change to domicile = 0 or not to write.

So

Conclusion 1: The Enum type should start from 0. When creating an enumeration, select the most reasonable default value and assign it a zero value. This means that as long as the enumeration is not explicitly assigned a value when it is created, all the enumerated values will have the default value.

Conclusion 2: any integer can be assigned to enum. For example, the code line site = (SITE) 10,
No errors are generated. However, this should not be done either, because the default convention is that enumeration variables only hold one of the enumerated values. Assigning any value to a variable of the enumeration type may lead to errors, for example, passing a value through the service.


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.