4

Today I was doing another Codegolf challenge over at Codegolf StackExchange, and I tried to do this:

SomeEnum = SomeCondition ? 1 : 2;

but this tells me Cannot convert source type 'int' to target type 'SomeEnum', so I tried this instead:

SomeEnum = SomeCondition ? (SomeEnum)1 : (SomeEnum)2;

Which then solved my problem, but to my surprise the first cast here is said to be redundant. My question is: Why do I only need to cast the last integer to SomeEnum?

Metoniem
  • 239
  • 2
  • 15

4 Answers4

6

The rules for the conditional operator are (C# Spec 7.14):

•   If x has type X and y has type Y then
o   If an implicit conversion (§6.1) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.
o   If an implicit conversion (§6.1) exists from Y to X, but not from X to Y, then X is the type of the conditional expression.
o   Otherwise, no expression type can be determined, and a compile-time error occurs.

In general, there is no implicit conversion in either direction between enums and ints.

So, why is your code working? Because in your actual code, the first value is 0, not 1. And so we get the one case where an integer can be implicitly converted to an enum (C# Spec 6.1.3):

An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type and to any nullable-type whose underlying type is an enum-type...

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • Thanks alot for your answer, I never knew about this and I had no idea the 0 would make a difference in my example. I understand now! :) – Metoniem Feb 07 '17 at 10:32
  • 2
    The moral of the story is don't post pseudo code, post a [minimal, complete and verifiable](http://stackoverflow.com/help/mcve) example ;-) – Equalsk Feb 07 '17 at 10:35
  • @Equalsk Yeah, that would have definitely prevented a bunch of confusion here. I'll keep it in mind! – Metoniem Feb 07 '17 at 10:39
3

You can write like this also

SomeEnum = (SomeEnum)(SomeCondition ? 1 : 2);

SomeEnum of left side always expecting the result as 'SomeEnum'. So we need to convert it.

andy
  • 5,979
  • 2
  • 27
  • 49
  • This is actually a non-confusing way to cast indeed, I'll definitely use it and I upvoted your answer, but it doesn't really answer my question completely! Thanks anyways :) – Metoniem Feb 07 '17 at 10:25
0

To bind the result to of ternary operator to SomeEnum, the result of both branches (true and false) should be able to be casted implicitly to SomeEnum, but there is no implicit cast operator by default to int defined for the enum type in c#. If there was implicit operator, this will be valid scenario.

I hope this explains the situation.

vasil oreshenski
  • 2,788
  • 1
  • 14
  • 21
  • The return type of the conditional operator is based solely on the types of the expressions it contains. It does not, as you claim, somehow depend on the result variable (in fact, in a large number of cases, when used as part of a larger expression, no "result variable" exists) – Damien_The_Unbeliever Feb 07 '17 at 10:17
  • Thansk for the correction, i've edited my answer. – vasil oreshenski Feb 07 '17 at 10:20
0

This compiles without any warning:

SomeEnum SomeEnum = (true) ? (SomeEnum)1 : (SomeEnum)2;

But you should assign the enum variable an enum value:

SomeEnum SomeEnum = (true) ? SomeEnum.First : SomeEnum.Second;

public enum SomeEnum : byte
{
    First = 1,
    Second = 2
}

The following should not compile:

SomeEnum SomeEnum = (true) ? (SomeEnum)1 : 2; //THIS WON'T COMPILE!

...unless the first value is 0:

SomeEnum SomeEnum = (true) ? 0 : 2;
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Look at [my code here](http://codegolf.stackexchange.com/a/109377/65012), it compiles fine while it seems to be almost identical to what you said shouldn't compile. I'm very confused. – Metoniem Feb 07 '17 at 10:23
  • *Almost* identical isn't the same as identical. The difference is that the code you originally posted sets the value to 1 instead of 0. Big difference as pointed out by the other answer. There is a reason why you should always provide a reproducible sample of your issue when you ask a question... – mm8 Feb 07 '17 at 11:56