Question of the day: Enums

Some fun with enums! Assume you have an enum defined as follows:

enum Frequency {
    None = 0,
    Annual = 1,
    SemiAnnual = 2,
    Quarterly = 4,
    Monthly = 12,
    Weekly = 52
}

Now, given the following block of code:

Frequency myFrequency = (Frequency) 104;
System.Console.WriteLine("My frequency is: {0}", myFrequency.ToString());
bool is100Defined = Enum.IsDefined(typeof(Frequency), 100);
bool is52Defined  = Enum.IsDefined(typeof(Frequency), 52);
  1. What is written to the console?
  2. What is the value of is100Defined?
  3. What is the value of is52Defined?

First correct answer gets absolutely nothing (but think how great you’ll feel!)

Posted in C#, Daily Question by Gerrod at October 7th, 2009.

2 Responses to “Question of the day: Enums”

  1. Rob E says:

    output will obviously be “My frequency is: 104″,
    is100Defined is false since there is no such 100 possible defined from the enums.
    is52Defined is true since it corresponds to Weekly.

    It gets more fun when you start using those enums to do complex bitwise comparisons using ~ | & in the same statement. I knew they were wrong, my philosophy of classical logic finally has a practical use!!

    Your right i do feel great.

  2. Drew says:

    I second that. But I don’t feel as great.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Quickduck logo