473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Casting of Nullable Enums

enum E = {a = 10, b = 10, c = 10};

object Value = 10;

E? En = (E?)(int?)Value;

Why do I need to caste it to (int?) before (E?)

On the other hand, if it is not nullable, it doesn't request to cast
to (int)

E En = (E) Value;

Thanks in advance.
Sep 1 '08 #1
1 9140
"CKKwan" <ck****@my-deja.comwrote in message
news:d2**********************************@v16g2000 prc.googlegroups.com...
enum E = {a = 10, b = 10, c = 10};

object Value = 10;

E? En = (E?)(int?)Value;

Why do I need to caste it to (int?) before (E?)
Because the boxed value is of type int, and not of your enum type. When
unboxing, you always have to unbox to the precise type of the value; any
conversions are not considered. Similarly, you can't box a float, and then
unbox it straight to an int, even though you can normally cast a float to a
nint.
On the other hand, if it is not nullable, it doesn't request to cast
to (int)

E En = (E) Value;
The fact that it works is actually incorrect, and it's more of a deficiency
in the existing .NET implementation than anything else. According to both
the language spec and the CLR spec, it shouldn't work. A similar case is
with a boxed int unboxable to uint and vice versa, even though they're two
distinct types.

Sep 1 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt | last post by:
Hi, Recently we had some code like this cause a failure: MyEnum myEnum = (MyEnum) (int) dt; i.e. reading an int out of the database and casting it into a type-safe enum. The thought...
1
by: Fei Li | last post by:
Hi, Whi can help to expain what logic is when I cast enum a to b? Thanks
4
by: DKode | last post by:
I started using Enums to make my code more readable. Here is my ENUM: public enum EntryType : int { RegularHours = 1, Lunch = 2, Vacation = 3, Sick = 4, Personal = 5 }
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
10
by: Tyler Durden | last post by:
Hi, I have this annoying problem with a cast failing from a DAL that was created in 1.1 that I have included in a 2.0 app. Every stored proc in our system uses an integer parameter to return...
2
by: John Smith | last post by:
Hi All, (I first posted this question on the csharp.general but got no response, and since this NG is more active, so I repost it here) I have two enum definitions from two different...
4
by: PokerMan | last post by:
Hi guys, Maybe someone can explain thisi have this enum: public enum LimitType : int { BottomLimit, TopLimit, Limit }
1
by: =?Utf-8?B?QkpT?= | last post by:
I have written a Generic method that takes an object, a type, T, and returns a System.Nullable (Of T) depending on whether or not the object IsDBNull. It was working fine until I tried to pass a...
5
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I have this snippet that don't compile: Math.Ceiling(-voceValue.Value - 0.5M); because the field Value of the class voceValue is decimal nullable. How can I solve? Thanks a lot.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.