CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: enum base types

  1. #1
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    enum base types

    I'm trying to find you what is the 'point' of specifying a base type in an enum? What is the benefit of using...
    Code:
    enum State : int  // note the 'int'
    {
    	Initialise = -1,
    	Process = 0,
    	Finalise = 1
    }
    over...
    Code:
    enum State
    {
    	Initialise = -1,
    	Process = 0,
    	Finalise = 1
    }
    One incorrect assumption that I made was to assume you would be able to do...
    Code:
    Stage currentStage = Stage.Initialise;
    int databaseField = currentStage;  // ultimately a field in a DataTable of type SqlDbType.Int
    but it seems you still need to cast...
    Code:
    Stage currentStage = Stage.Initialise;
    int databaseField = (int)currentStage;
    So what is the reason for specifying a base type?

    I do realise the 'int' is the default, I'm just talking in general. All I can think of is compiler optimisation or early binding of errors maybe...
    Last edited by rliq; December 10th, 2008 at 06:34 PM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: enum base types

    PArtially guessing, but all "enum" declared items will inherit from System.Enum. If you LOOK at your class, what does it show as the ACTUAL Type and does it show a Base Type


    [I am aways from my compiler at the moment - I love WiFi on the train!!!]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: enum base types

    When the value assigned to an enumerator falls outside the range an signed integer (int) you'll have to specify a different base-type.

    This won't compile:
    Code:
    enum Range { A = 3000000000 };
    This will:
    Code:
    enum Range : long { A = 3000000000 };
    - petter

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: enum base types

    Got it....

    Technically it is NOT strictly a base class (although it uses that syntax).

    From the documentation

    Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int. To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type:

    The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is necessary to convert from enum type to an integral type. For example, the following statement assigns the enumerator Sun to a variable of the type int by using a cast to convert from enum to int:

    In this example, the base-type option is used to declare an enum whose members are of the type long. Notice that even though the underlying type of the enumeration is long, the enumeration members must still be explicitly converted to type long by using a cast.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: enum base types

    CPU: The default base type is 'int'.
    FROG: Yeah, early detection of errors and maybe compiler optimisation.

    Just weired I had to cast, as it must always convert to an 'int' or whatever base type is specified (as shown in Frog's example).
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  6. #6
    Join Date
    Mar 2004
    Location
    3311'18.10"N 9645'20.28"W
    Posts
    1,808

    Re: enum base types

    enums are strongly typed flags. because they're type is the name you named the enum, you have to cast it to whatever value you have because its inherent value is the name of the flag you specified (and not the value you assigned that field).

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: enum base types

    Quote Originally Posted by rliq View Post
    CPU: The default base type is 'int'.
    THe underlying type is by default int, as I posted previously. It is NOT truely a "Base class" which inspection of the CLR source quickly shows (it is also to dynamically change the base class of any class, even with the use of generics!!!)....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: enum base types

    I think we posted those two comments at the same time.... I was just quoting O'Reilly C# 3rd edition, but I understand your meaning. My original question, was more of a 'what the!?' than a 'how do you?'. Thanks for the posts guys.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  9. #9
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: enum base types

    Quote Originally Posted by rliq View Post
    So what is the reason for specifying a base type?
    (With the exception that it is not exactly base type, but more accurately underlying type) I would say that this has no practical usage in pure C# application, but it can be utilized in communication with unmanaged environment, e.g. marschalling or P/Invoke, allowing you to match exactly the unmanaged world's structure.

    But that is just my guess, a "bet" into the debate.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: enum base types

    Quote Originally Posted by boudino View Post
    (With the exception that it is not exactly base type, but more accurately underlying type) I would say that this has no practical usage in pure C# application, but it can be utilized in communication with unmanaged environment, e.g. marschalling or P/Invoke, allowing you to match exactly the unmanaged world's structure.

    But that is just my guess, a "bet" into the debate.
    It has the EXACT same benefit as deciding between byte, sohrt, int, long when declaring variables. MEMORY utilization. If you have an array of a few million enums [or a few million instances each with a few enums] it is "silly" two use 4 bytes for something that has <128 values (I am ducking the sign issue)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)