tags:

views:

87

answers:

4

If I have the enum:

public enum VehicleType
{
    Car = 0,
    Boat = 1,
    Bike = 2,
    Spaceship = 3
}

and I then do:

int X = 10;
VehicleType vt = (VehicleType)2;
X = X + vt;
Console.WriteLine("I travel in a " + vt + " with " + X + " people.");

What should the output be in C#?

A: 

An enum's base type is int by default. It can also be a byte, sbyte, short, ushort, uint, long, or ulong if explicitly specified.

X = X + vt will error because it needs to be an explicit cast.

If it were X += (int)vt; it would be:

"I travel in a Bike with 12 people."

because when using Console.WriteLine all variables' ToString() methods are called so the string representation of the Enum is given (enum is 2, that equates to Bike, so Bike is returned).

David Neale
My enums are evaluating to strings where it would suit. ie. I travel in a Bike with 12 people.
Craig Johnston
@-1er: why the downvote?
David Neale
@Neale: the answer is incorrect. If someone is going to answer a question like this they should run the code through a compiler if they are not sure.
Craig Johnston
Have run the code - your code will not compile.
David Neale
http://msdn.microsoft.com/en-us/library/sbbt4032%28VS.80%29.aspx - C# forces explicit enum casting.
David Neale
You are correct that the X = X + vt will not compile because it requires an explicit cast, but you are incorrect in saying that enum casts to an int by default because enums will cast to string when concatenated to a string.
Craig Johnston
Fair enough - when ToString() is called on an Enum then a string is represented. I have implied this above by saying "Bike" is written. This is not casting! Try (string)vt - it won't work because you cannot cast an enum to a string. Learn what casting is before you start downvoting valid answers.
David Neale
upvoted to compensate downvote of correct answer
Oliver
@Neale: your edit of the your answer makes it the best answer here, but I the answers that say that enums a represented as int are still not correct.
Craig Johnston
@Neale: I will accept this answer, only on the basis that the explicit cast is required for the x = x + vt line, not that an explicit cast is required all the time since the Console.WriteLine is compiled without such a cast.
Craig Johnston
Thanks Craig - I think you may misunderstand the concept of casting. It is a very precise term, the above is technically correct (and this is a technical forum). The arithmatic part needs an explicit cast, the other explicitally makes a call to ToString(), it is important to understand what the code is doing.
David Neale
Yes, I was a bit loose with the term 'casting'. It does appear that Enums require explicit casting, however, contrary to 2 other answers, enums are not represented as ints, they are just Enums. That is why the concat function can utilise the ToString() to access the string part of the enum.
Craig Johnston
A: 

They are represented as integers. I wish they could be represented as objects!

Aggelos Mpimpoudis
@-1er: why the downvote?
David Neale
@Neale: the answer is incorrect. If someone is going to answer a question like this they should run the code through a compiler if they are not sure.
Craig Johnston
This is not incorrect - it is a true statement following by a valid opinion.
David Neale
They are not represented as integers when they concatenated to strings, so the answer is incorrect.
Craig Johnston
I see your point, this looks more to be a misunderstanding of your question. An enum will never *cast* itself to anything other than its integral type. However, calling ToString() will return the string value.
David Neale
upvoted to compensate downvote of correct answer
Oliver
@Oliver: it is not a correct answer. Enums when concatenated to strings are not represented as ints, so this answer is incorrect.
Craig Johnston
Agreed. ToString() is implicitly called on variables when strings are concatenated and the base type (int) is not returned, the string representation is.
David Neale
+6  A: 

In X = X + vt; vt will be casted to int. In "I travel in a " + vt + " with " + X + " people." vt will be replaced to vt.ToString(), which will print the name of the enum.

Al Bundy
[In X = X + vt; vt will be casted to int] - no it won't. There is no implicit casting method on Enums and they are sealed by the compiler so one cannot be implemented. An explicit cast is needed: X = X + (int)vt;
David Neale
http://msdn.microsoft.com/en-us/library/sbbt4032%28VS.80%29.aspx - C# forces explicit enum casting.
David Neale
@Neale: When I try something similar in a c# compiler I am getting what this answer says so I think it is correct. Have you tried this in a compiler?
Craig Johnston
Try compiling the exact code you have in your question - the X = X + vt; line will not compile.
David Neale
If X = X + vt doesn't compile then how could vt be represented as an integer as per the other answers?
Craig Johnston
The compiler blocks the implicit conversion from the Enum to its base class (by default, int). From MSDN: "The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type to an integral type."
David Neale
I have deselected this answer because it x = x + vt doesn't compile without an explicit cast, however I cannot accept any of the other answers because they say that enums are represented as integers which is incorrect because enums cast to string when concatenated to strings.
Craig Johnston
They do not cast to string! I've amended my answer to make it clearer.
David Neale
Ok, I'm not that concerned about whether the Enum is cast or whether the concat function calls toString(). One thing's for certain: the enum is not represented as an int because (2).ToString() != "Bike". Vt is never an int in the Console.Write line, it is just an Enum.
Craig Johnston
A: 

As the others already mentioned you just get an integer.

That's because the base-type of an enum is an integer, but this can be changed to any other value type by e.g. public enum VehicleType : ushort.

To get some better handling with these names the Enum class has some handy functions (like GetName(), IsDefined() or Parse()).

Just to answer the question So how can you say that the enum is being represented as an int in what you have just stated? from the comments: Take a look at http://msdn.microsoft.com/en-us/library/sbbt4032.aspx (especially take a decent look into the last example using Flags).

Oliver
@-1er: why the downvote?
David Neale
@Neale: the answer is incorrect. If someone is going to answer a question like this they should run the code through a compiler if they are not sure.
Craig Johnston
There's nothing incorrect in this answer.
David Neale
@Craig: which of the code in my answer is incorrect?
Oliver
Then why are there 6 votes for the selected answer and no votes for any others?
Craig Johnston
That doesn't make the other answers incorrect. In fact the selected answer is the only one that is wrong (in part)!
David Neale
Ok, it seems you are correct about explicit casting being required for the line: X = X + vt; However, that makes all the answers incorrect including yours, because Enums will implicitly cast to Strings. Try it.
Craig Johnston
That's not casting. Using Console.Writeline will mean that the Enum's ToString() method is called. This does indeed return the Name as stated. None of the answers are incorrect aside from the selected one.
David Neale
If ToString() is being called then is it Enum.ToString() or int.toString()? When does an enum become an int in a string concatenation?
Craig Johnston
It always has the base type of an int. That's a good reason for using enums, because they are small and efficient. The Enum type you have created will work out what the ToString() return value should be based on the int (2) and the enum type.
David Neale
So (2).toString() = "Bike"?
Craig Johnston
No - ((VehicleType)2).ToString(); = "Bike"
David Neale
So how can you say that the enum is being represented as an int in what you have just stated?
Craig Johnston
I haven't said that it's represented as an int. Your question was misunderstood by everybody. A more concise version of your question should be "What does ToString() return when called on an enumeration".
David Neale
You said "the selected answer is the only one that is wrong", when in fact there are several answers here that are wrong because they said that Enums are represented as ints, when they are really represented as Enums. That is why the string concat function accepts them as Objects and calls the ToString() function.
Craig Johnston
@Oliver: unless you can tell me how (2).ToString() = "Bike", there is no way that the Enum in the Console.WriteLine line is being represented as an int. How could it? How could the result of ToString() on an int produce "Bike"?
Craig Johnston
@Craig: you see it the wrong way. An enum has always a base value type (normally int), but that doesn't mean that a value type is automatically an enum. So what works is VehicleType vt = (VehicleType)(int)2; (The int cast is just to show you that 2 is really an integer). And it goes also the other way arount: int value = (int)VehicleType.Bike;
Oliver