Will this code compile? Does it? What does it mean?

using System;

class Test
enum Foo { Bar, Baz };

static void Main()
Foo f = 0.0;

 Posted by Kmandapalli on 2/19/2014 | Category: ASP.NET Interview questions | Views: 2374 | Points: 40

This shouldn't compile, but it does under the MS compilers for both C# 2 and 3 (and probably 1 as well - I haven't checked). It shouldn't compile because only the literal 0 should be implicitly convertible to the default value of any enum. Here the decimal is 0.0. Just a little compiler bug. The result is to print Bar as that's the 0 value of the Foo.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response