Click here to Skip to main content
12,406,795 members (60,019 online)

Welcome to the Lounge

   

For lazing about and discussing anything in a software developer's life that takes your fancy except programming questions.

Technical discussions are encouraged, but click here to ask your programming question.

The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. No flame wars, no abusive conduct, no programming questions and please don't post ads.
 
GeneralDoes this language feature seem strange to anyone else? Pin
lewax0020-Mar-13 12:58
memberlewax0020-Mar-13 12:58 
Part of an application I work on involves navigation through a series of documents, previously the navigate function looked like:
void NavigateTo(int document)
but as part of a redesign that made a single numeric index lose it's meaning, I changed it to
void NavigateTo(NavigationDirection direction)
where NavigationDirection is an enum consisting of Forward and Backward. As expected, the change caused some compilation errors where it was being called, because C# doesn't allow implicit conversion of int to enum. Having written the section of code it was in, I was pretty sure I knew where all the calls were, and the compiler seemed to agree...but the navigation was acting funny, it started at the second document. Debugged it, and found a third call to the function, that looked like this:
NavigateTo(0);
and 0 was the value of Forward, which was then moving it to the 2nd document, added a third value to the enum, None, since the first document was already being loaded in a new way, and this was just moving past it (the navigation has some side effects, loading other things related to the document, so the call was still needed...it makes more sense in the actual code, I'm just simplifying it). But hold on, I thought there wasn't an implicit conversion from int to enum types in C#?

So I played around with it a bit, and here's what I discovered: ints are not implicitly convertible to enum types (as I thought)...but const ints are 0 is! Both integer literals, and const int variables, as long as the value maps to a value in the enum (if it had been a number greater than 1 other than 0, I would have had the expected compiler error). While I understand how the compiler can implement this, I don't understand why they would, it seems to go against the C# language itself. I don't think you could even write your own implicit conversion that behaves the same way, since from what I can tell C# doesn't generally make a distinction between const and non-const versions of the same type for the purposes of parameters (e.g. int and const int can be used interchangeably), with the exception of ref and out...but that causes restriction in the reverse direction (only non-const).

Seems like an odd language design choice to me D'Oh! | :doh:

EDIT: looks like I jumped the gun a little on this one, apparently, it only works for 0. Which is probably worse...
GeneralRe: Does this language feature seem strange to anyone else? Pin
PIEBALDconsult20-Mar-13 13:43
memberPIEBALDconsult20-Mar-13 13:43 
GeneralRe: Does this language feature seem strange to anyone else? Pin
lewax0020-Mar-13 14:00
memberlewax0020-Mar-13 14:00 
GeneralRe: Does this language feature seem strange to anyone else? Pin
AspDotNetDev20-Mar-13 14:04
protectorAspDotNetDev20-Mar-13 14:04 
GeneralRe: Does this language feature seem strange to anyone else? Pin
lewax0020-Mar-13 14:11
memberlewax0020-Mar-13 14:11 
GeneralRe: Does this language feature seem strange to anyone else? Pin
PIEBALDconsult20-Mar-13 14:17
memberPIEBALDconsult20-Mar-13 14:17 
GeneralRe: Does this language feature seem strange to anyone else? Pin
Matthew Faithfull20-Mar-13 14:10
memberMatthew Faithfull20-Mar-13 14:10 
GeneralRe: Does this language feature seem strange to anyone else? Pin
lewax0020-Mar-13 14:13
memberlewax0020-Mar-13 14:13 
GeneralRe: Does this language feature seem strange to anyone else? Pin
Matthew Faithfull20-Mar-13 14:29
memberMatthew Faithfull20-Mar-13 14:29 
GeneralRe: Does this language feature seem strange to anyone else? Pin
lewax0020-Mar-13 14:39
memberlewax0020-Mar-13 14:39 
GeneralRe: Does this language feature seem strange to anyone else? Pin
Matthew Faithfull20-Mar-13 15:10
memberMatthew Faithfull20-Mar-13 15:10 
GeneralRe: Does this language feature seem strange to anyone else? Pin
Mike Winiberg21-Mar-13 22:14
memberMike Winiberg21-Mar-13 22:14 
GeneralRe: Does this language feature seem strange to anyone else? Pin
AspDotNetDev20-Mar-13 13:56
protectorAspDotNetDev20-Mar-13 13:56 
GeneralRe: Does this language feature seem strange to anyone else? Pin
lewax0020-Mar-13 14:06
memberlewax0020-Mar-13 14:06 
GeneralRe: Does this language feature seem strange to anyone else? Pin
AspDotNetDev20-Mar-13 14:09
protectorAspDotNetDev20-Mar-13 14:09 
GeneralRe: Does this language feature seem strange to anyone else? Pin
jschell21-Mar-13 10:04
memberjschell21-Mar-13 10:04 
GeneralRe: Does this language feature seem strange to anyone else? Pin
lewax0021-Mar-13 10:27
memberlewax0021-Mar-13 10:27 
GeneralRe: Does this language feature seem strange to anyone else? Pin
César de Souza22-Mar-13 0:43
memberCésar de Souza22-Mar-13 0:43 
GeneralRe: Does this language feature seem strange to anyone else? Pin
lewax0022-Mar-13 10:58
memberlewax0022-Mar-13 10:58 
GeneralRe: Does this language feature seem strange to anyone else? Pin
englebart22-Mar-13 2:26
memberenglebart22-Mar-13 2:26 
GeneralRe: Does this language feature seem strange to anyone else? Pin
lewax0022-Mar-13 10:41
memberlewax0022-Mar-13 10:41 
GeneralRe: Does this language feature seem strange to anyone else? Pin
RafagaX22-Mar-13 6:40
memberRafagaX22-Mar-13 6:40 
GeneralRe: Does this language feature seem strange to anyone else? Pin
Chadwick Posey22-Mar-13 7:48
memberChadwick Posey22-Mar-13 7:48 
GeneralRe: Does this language feature seem strange to anyone else? Pin
lewax0022-Mar-13 10:45
memberlewax0022-Mar-13 10:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.8.160730.1 | Last Updated 31 Jul 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid