none
Type-Safe Enum Design Pattern RRS feed

  • Question

  • I have not been able find such kind of thing on msdn or in the Microsoft knowledge base.

    Any reference ? 

    Thanks.


    usher A

    Tuesday, March 12, 2019 9:14 PM

All replies

  • Hi usher227,

    Thank you for posting here.

    Based on my search, I do not find the MSDN document. But you could check the links below. It maybe helpful.

    https://stackoverflow.com/questions/10100350/how-can-i-use-switch-statement-on-type-safe-enum-pattern

    https://www.infoworld.com/article/3198453/how-to-implement-a-type-safe-enum-pattern-in-c.html

    Best Regards,

    Wendy

    Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you. 
    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, March 13, 2019 5:33 AM
    Moderator
  • Depends upon what you need by a type-safe enum design. An enum is a named integral constant effectively (although other types are support in a limited manner). As such most languages (including C# and C++) don't provide the necessary infrastructure to make enums truly type-safe. You can always convert enums from/to ints and there isn't anything you can do about it except do range checking but that is error prone at best. Short of writing your own enum-like type (which is probably not going to be easy or performant) you are stuck with enums working the way they do.

    Can you provide a more detailed explanation of what you're trying to do? Perhaps enums aren't the best choice for your problem.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, March 13, 2019 1:48 PM
    Moderator
  • I am not trying to do anything . 

    I just wanted to know if this pattern is an official pattern employed by Microsoft. 

    I just came across it and was wondering if it is worth using it and why if the inventor of C# i mean MS has not even mentioned it any where on msdn.

    Thanks


    usher A

    Wednesday, March 13, 2019 4:11 PM
  • Thanks.

    I already am familiar with those links. But my qs is why to use it at all when Microsoft does not even document it.

    Somewhere in our code someone used it . I think he came from java back ground and they use it quite often. 

    Correct me if I am wrong .

    Thanks.


    usher A

    Wednesday, March 13, 2019 4:14 PM
  • It's a design pattern so yes there are probably many places in .NET that use it but as a design pattern I wouldn't say there is any "official" pattern. Patterns are just common ways to solve common problems.  MSDN doesn't document the many design patterns that are available because it isn't necessary. You can google and find sites dedicated to this stuff. The only patterns that MS tends to discuss are those that are intimately tied to an implementation such as unit of work for EF or DI for ASP.NET Core.

    In C# this pattern could be implemented using a static class with static fields and that is done frequently in code. The Colors type in System.Drawing is a good example. This struct exposes readonly fields for each of the standard colors that are available. MS could have used an enum where the enum values are the RGBA equivalent values but then code that wants to support colors would need to support the core Colors struct and also this enum which means they'd need to support integral values as well. Of course for colors this wouldn't have been a big deal at all because all possible RGBA values would work but to get the underlying value you'd be doing casts to int. Readonly fields made more sense in this case because the Color struct was already available anyway.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, March 13, 2019 4:33 PM
    Moderator
  • Depends upon what you need by a type-safe enum design. An enum is a named integral constant effectively (although other types are support in a limited manner). As such most languages (including C# and C++) don't provide the necessary infrastructure to make enums truly type-safe. You can always convert enums from/to ints and there isn't anything you can do about it except do range checking but that is error prone at best. Short of writing your own enum-like type (which is probably not going to be easy or performant) you are stuck with enums working the way they do.

    Can you provide a more detailed explanation of what you're trying to do? Perhaps enums aren't the best choice for your problem.


    Michael Taylor 

    >> You can always convert enums from/to ints and ...

    Let me rephrase it for you: You can always convert a variable of type enum to its underlying type, and vice-versa.

    Sure! This is by design.
    >> and there isn't anything you can do about it ...

    It's possible: just don't implement any explicit type casting!

    Using implicit type casting, will not compile.

    >> C# doesn't provide the necessary infrastructure to make enums truly type-safe

    Please, give us 1 example where the type enum is "not type safe"; just 1 is enough!


    • Edited by ritehere44 Thursday, March 14, 2019 1:54 AM
    Thursday, March 14, 2019 1:51 AM
  • > I just wanted to know if this pattern is an official pattern employed by Microsoft.

    Really, who cares?  No one is asserting that Microsoft's developers are any kind of authority on programming patterns and standards.  If the pattern makes sense for you and makes development easier, then use it.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Thursday, March 14, 2019 6:50 AM
  • As such most languages (including C# and C++) don't provide the necessary infrastructure to make enums truly type-safe


    Michael Taylor 

    It's very important to all members of this forum, you give us 1 example (C# only) that show us the type enum is not type safe, to corroborate your assertion above.

    C'mon, just 1 example is enough.

    Thursday, March 14, 2019 7:43 AM
  • Hi there,

    @ritehere44  Can you explain what you mean? sorry but, i guess you dont have much expirience with that what you posted. 

    Best wishes ...

    Thursday, March 14, 2019 11:53 AM
  • As such most languages (including C# and C++) don't provide the necessary infrastructure to make enums truly type-safe


    Michael Taylor 

    It's very important to all members of this forum, you give us 1 example (C# only) that show us the type enum is not type safe, to corroborate your assertion above.

    C'mon, just 1 example is enough.

    See ex mvp @Michael, you was not able to post even one single example!
    And we all know why: once more, you insist in posting concept that doesn't exist in official documentation (or any text book). 
    It's just one more rubbish# concept that grows in your mind, and you don't care if it's correct or not (forum people are learning wrong concepts, and you don't care). 
    Clearly (at least for me), you don't understand, precisely, what an enum type is, hence, your assertion: "enum type is not type safe."
    It's regrettable someone registered for 15+ years in this site (and this specific forum), still be so ignorant about C# language.
    The way I see you on this site, implies in camaraderie or nepotism (or maybe both), because you'd fail any knowledge testing in C#.
    Accept my suggestion: start asking questions!

    Thursday, March 14, 2019 11:45 PM
  • I have not been able find such kind of thing on msdn or in the Microsoft knowledge base.

    Any reference ? 

    Thanks.


    usher A

    Hi,

    your question does not make sense, because the type enum is not unsafe; also, you are using the expression "design pattern", out of context.

    Much probably, you are looking for best practices when using the type enum.

    • Proposed as answer by ritehere44 Thursday, March 14, 2019 11:52 PM
    • Unproposed as answer by CoolDadTxModerator Friday, March 15, 2019 1:49 AM
    Thursday, March 14, 2019 11:51 PM
  • Greetings Ritehere44.

    I think what CoolDadTx is getting at is that, because enums can be cast to ints, it's theoretically possible to do something like this;

          enum Numbers { Zero, One, Two, Three };
    
          static void Main(string[] args)
          {
             Numbers number1 = (Numbers)1;
             Numbers number10 = (Numbers)10; // This doesn't crash, even though 10 isn't in the enum.
    
             Console.WriteLine(number1.ToString());
             Console.WriteLine(number10.ToString()); // This doesn't crash either.
    
             // Output is;
             //
             // One
             // 10
          }
    

    Strictly speaking, this does not meet my definition of "not typesafe", but I know what he means. Anyone dumb enough to try something like casting ints to enums could cause chaos.

     

    Usher227,

    In my opinion the pattern you refer to is unwieldy and confusing, and no programmer worth his ones and zeros would be in danger of misusing enums badly enough to need it anyway. I'm not surprised that Microsoft doesn't make it official.

    Friday, March 15, 2019 3:17 AM
  • > I just wanted to know if this pattern is an official pattern employed by Microsoft.

    Really, who cares?  No one is asserting that Microsoft's developers are any kind of authority on programming patterns and standards.  If the pattern makes sense for you and makes development easier, then use it.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    I like that. It doesn't matter who designed a pattern...

    PS:

    >> ...posting concept that doesn't exist in official documentation (or any text book).

    What do you mean by that? It's not allowed to design new patterns which are not in any book? That's cheap... If everyone thinks like you, IT would now be like 20 years ago! I hate this attitude!

    Friday, March 15, 2019 9:01 AM
  • This is for everyone .

    Let's make it a discussion not a heated argument. No one can prove who is better than anyone . 

    I realized Java people use this pattern more often than Microsoft . But anyway I am not going to use this pattern because I know in advance about  enum constants . I won't be doing anything wrong or giving access to anyone for modification .

    Thank you all 


    usher A

    Thursday, March 21, 2019 4:10 PM