Take the 2-minute tour ×

This question already has an answer here:

Since enums are value type and internally integers, is it less expensive to cast them to an int than a cast normally is?

If it is faster, is it because it's value type and not reference type?

share|improve this question

marked as duplicate by Ken White, Eric Brown, p.s.w.g, talonmies, icktoofay Jul 24 '13 at 5:11


Why don't you measure it? –  spender Jul 24 '13 at 0:15
Read Jon Skeet's answer here stackoverflow.com/questions/3256713/enum-and-performance –  lahsrah Jul 24 '13 at 0:16
And after you read Jon Skeet's answer, read Eric Lippert's blog post: ericlippert.com/2012/12/17/performance-rant –  Alden Jul 24 '13 at 0:17
I'd venture that your time might be better spent on higher level issues than asking about the (negligible) cost of casting without a particular problem in mind. You should always code for clarity rather than convoluting your code to gain some unmeasured time. You'll end up with complicated code that probably won't run any faster. It's next to impossible to know where your code will "run slow". Don't try and guess. Measure once you've written correct code, then act accordingly. –  spender Jul 24 '13 at 0:27
You either have an unhealthy obsession with premature optimization, or your class is studying something related to that topic currently. If it's the first, learn to write the code first, profile it, and then optimize the actual bottlenecks in your code instead of worrying about non-issues like this one. (Anyone who worked for me that wasted time on silly unnecessary optimization of code wouldn't last long; there are productive things to do with that time instead.) If it's the latter, do your homework instead of asking the questions here and waiting for us to answer them. :-) –  Ken White Jul 24 '13 at 0:54

1 Answer 1

No, because of the way it stores the value internally. There is virtually no C# overhead. The overhead in casting is largely due to the amount of code necessary to go from one type to another, does a new object have to be created and allocated memory, etc...

share|improve this answer
