c# - Casting Enum to uint -


I am writing a function that takes Enum and put it on uint . When casting while I cast to int , I've already added what you've seen in something: (int) (object) myEnumValue . If you type myEnValue , you get a compiled time exception.

Now, when I tried to put it in the UIT, I expected that (UIT) (object) my anonymize would be okay. It compiles well, but on the run, it creates a InvalidCastException , to work it, I

  (uint) (int) (object ) I have used my Anonymous value  

I think it looks ridiculous, so I am very happy, but why is it so? Perhaps it would be better to ask why it is not possible to put object to uint , but I am interested in whether the There is another way to go from Enum to uint . Is there?

Edit:

The reference is a function, something like this:

  Public static UIT toUuent (Anum E) {Return (UIT) (int) (object) e; }   

Edit 2:

The best solution was written by:

  Convert ToUInt32 (e)  

(UIT) (object) myEnum approach fails because, by default, C # arrays use int as their underlying type, and a int is when they are boxed There are C # syntax as these anhms are their underlying type (like enum MyEnum: receive uint ).

You must explicitly specify the compiler object before int first, then int to uint Make a numerical conversion on. (Although the syntax is the same, there is a separate process of casting between object from a value type to unboxing int and uint ).


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -