locked
Design question: Default value for enum RRS feed

  • Question

  • Hi All,

    Is it a good design practice to have a default value for every enum I create. I have an enumeration as the following.

        public enum OperationType
        {
            Action,
            StartTask
        }

    Is it a better design practice to change the enumeration to

        public enum OperationType
        {
            None,
            Action,
            StartTask
        }

    Thanks in advance for your time and help.

    Thanks,

    Venkat


    Tuesday, November 20, 2012 12:09 AM

Answers

  • Hi,

    http://msdn.microsoft.com/en-us/library/ms182149%28v=vs.80%29.aspx

    This may help you.

    Thanks.

    • Proposed as answer by Jason Dot Wang Wednesday, November 21, 2012 5:26 AM
    • Marked as answer by Venk K Monday, November 26, 2012 11:11 PM
    Tuesday, November 20, 2012 5:28 AM
  • Hi,

    See this, if you find it helpful

    By default, the underlying type of an enum is int. This default may be changed by specifying a specific base when declaring the enum. You would specify a different base if the enum was used extensively and there was an opportunity for space savings by selecting a smaller type. Another reason may be if you wanted the underlying type of the enum to correspond to another type in your program and you wanted to explicitly cast between the two without loss of precision. Valid base types include byte, sbyte, short, ushort, int, uint, long, and ulong.

    public enum Volume : byte
    {
        Low = 1,
        Medium,
        High
    }
    

    Go through this link also

    http://www.csharp-station.com/Tutorials/lesson17.aspx

    Note: From the artical above we can conclude that if you take your enum values other then 0,1,2 like 2000,7999 thay will take more space in the memory.

    Although this will take very less space, but it's good programming habit to declare enum with default values 0,1,2 and the underlying type of enum should be "sbyte" as it consumes less resources.


    Please mark as answer, if you find this post helpful. Thanks Deepak Kalra

    • Proposed as answer by Deepak Kalra Tuesday, November 20, 2012 6:52 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 28, 2012 8:56 AM
    Tuesday, November 20, 2012 6:52 AM
  • I also would never have any value in an enum that doesn't make sense.

    Also remember you can have nullable enums:

    Gender? foo = null;

    But overdooing this can also be wierd.
    • Proposed as answer by Jason Dot Wang Wednesday, November 21, 2012 5:25 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 28, 2012 8:56 AM
    Tuesday, November 20, 2012 3:39 PM
  • That makes me think that if you had "None" as a enum member, thigs could get more weird.

    Gender? g1 = null;
    Gender? g2 = Gender.None;
    Console.WriteLine(g1 == g2);

    As this (didn't test it) prints false.


    João Miguel

    • Proposed as answer by Jason Dot Wang Wednesday, November 21, 2012 5:26 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 28, 2012 8:56 AM
    Tuesday, November 20, 2012 3:50 PM

All replies

  • Yes and it's good practice to make "None" = 0.
    Tuesday, November 20, 2012 12:37 AM
  • Hi Pantelis44999,

    Thank you for your suggestion. Even I also feel the same although I cannot list down the actual reasons for that. Can you please send me some articles that explain the exact reason for doing this.

    Thanks,

    Venkat

    Tuesday, November 20, 2012 5:19 AM
  • Hi,

    http://msdn.microsoft.com/en-us/library/ms182149%28v=vs.80%29.aspx

    This may help you.

    Thanks.

    • Proposed as answer by Jason Dot Wang Wednesday, November 21, 2012 5:26 AM
    • Marked as answer by Venk K Monday, November 26, 2012 11:11 PM
    Tuesday, November 20, 2012 5:28 AM
  • Greetings Venk.

    It's only appropriate to have an enum value of "None" in cases where such a value actually makes sense. For example, consider the following.

    enum Gender
    {
       None,
       Male,
       Female
    }

    If Gender is being applied to human beings, a value of None is ridiculous, so there's no point in having it. In the case where you might want to indicate that a person's gender is not known or hasn't been input, you could have a third option, but give it a more meaningful name like "Unknown" or "NotInput".

    The only case where it's generally considered necessary to have a value of None set to zero is where the values are such that they can be combined bitwise and a value of zero indicates that nothing has been set.

    // A character may have any one, two, or more super powers, or none at all.
    enum SuperPowers
    {
       None = 0,
       SuperStrength = 1,
       XRayVision = 2,
       HeatRayVision = 4,
       CanFly = 8,
       CanBreathUnderwater = 16
    }
    Tuesday, November 20, 2012 5:57 AM
  • Hi,

    See this, if you find it helpful

    By default, the underlying type of an enum is int. This default may be changed by specifying a specific base when declaring the enum. You would specify a different base if the enum was used extensively and there was an opportunity for space savings by selecting a smaller type. Another reason may be if you wanted the underlying type of the enum to correspond to another type in your program and you wanted to explicitly cast between the two without loss of precision. Valid base types include byte, sbyte, short, ushort, int, uint, long, and ulong.

    public enum Volume : byte
    {
        Low = 1,
        Medium,
        High
    }
    

    Go through this link also

    http://www.csharp-station.com/Tutorials/lesson17.aspx

    Note: From the artical above we can conclude that if you take your enum values other then 0,1,2 like 2000,7999 thay will take more space in the memory.

    Although this will take very less space, but it's good programming habit to declare enum with default values 0,1,2 and the underlying type of enum should be "sbyte" as it consumes less resources.


    Please mark as answer, if you find this post helpful. Thanks Deepak Kalra

    • Proposed as answer by Deepak Kalra Tuesday, November 20, 2012 6:52 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 28, 2012 8:56 AM
    Tuesday, November 20, 2012 6:52 AM
  • Hi Venkat,

    It is good to have an enum with default value and it depends on your need as well.

    Say for example, if you want to map the datatable column value with enum and column value may have DBNULL.Value. For this kind of scenario you may need to map the default value of the enum as 'None/Default'.


    Thanks in Advance Suresh M
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Tuesday, November 20, 2012 3:05 PM
  • I also would never have any value in an enum that doesn't make sense.

    Also remember you can have nullable enums:

    Gender? foo = null;

    But overdooing this can also be wierd.
    • Proposed as answer by Jason Dot Wang Wednesday, November 21, 2012 5:25 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 28, 2012 8:56 AM
    Tuesday, November 20, 2012 3:39 PM
  • That makes me think that if you had "None" as a enum member, thigs could get more weird.

    Gender? g1 = null;
    Gender? g2 = Gender.None;
    Console.WriteLine(g1 == g2);

    As this (didn't test it) prints false.


    João Miguel

    • Proposed as answer by Jason Dot Wang Wednesday, November 21, 2012 5:26 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 28, 2012 8:56 AM
    Tuesday, November 20, 2012 3:50 PM