The necessity of the C #----Type () method for problems encountered with enumerations

Source: Internet
Author: User

I have defined an enumeration of AttributeName

Then write the following code:

Enum. GetValues (AttributeName);

There is no doubt that it is wrong. People say they want to add a typeof.

That is, enum. GetValues (Typeof (AttributeName));

So here's the question, why use the typeof operator?

Typeof (XX) Returns an object of type XX, Typeof (AttributeName) is an object that returns a AttributeName type.

( refer to link https://msdn.microsoft.com/zh-cn/library/58918ffs)

This means that the AttributeName is not an object, just a name or a attributename type (the same for enumerations)! Is the same for enumerations.

The parameter information for the GetValues () method is shown here:

public static Array GetValues (Type enumtype)
The requirement parameter is
Type Enumtype is an object of enum type! Yes, it's the object, Y.
Handling enumeration-related issues, the typeof operator is really essential!

The first essay, pure personal experience very much hope that the Tongren can refer to the lack of batch leakage, but also hope to hold the problem to the students to help.

The necessity of the C #----Type () method for problems encountered with enumerations

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.