Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do .net FlagsAttribute enums need to be manually valued?

To allow for different formatting options on a method that displays a news story, I've created an enumeration that can be passed in to specify how it gets displayed.

[Flags]
private enum NewsStyle
{
    Thumbnail = 0,
    Date = 1,
    Text = 2,
    Link = 4,
    All = 8
}

string FormatNews( DataRow news, NewsStyle style )
{
    StringBuilder HTML = new StringBuilder();

    // Should the link be shown
    if ( ((newsStyle & NewsStyle.All) == NewsStyle.All || (newsStyle & NewsStyle.Link) == NewsStyle.Link))
    {
                HTML.AppendFormat("<a style=\"text-decoration:none; color: rgb(66, 110, 171);\" href=\"ViewStory.aspx?nsid={0}\">",
                                  UrlEncode(newsStory["NewsStoryID"].ToString()));
    }

    // Etc etc...
}

// So to call the method...
Response.Write( FormatNews( news, NewsStyle.Date | NewsStyle.Text ) );

The problem is that I can only get the code to work if I manually specify the values on the enum, otherwise the bitwise enum checking operation doesn't work correctly.

I've always followed the rule of let .net handle the assigning of values to enums - is this a geniuine exception?

like image 973
Peter Bridger Avatar asked Feb 28 '23 03:02

Peter Bridger


1 Answers

Yes, it is a genuine exception. Enumerations are by default assigned values from 0 onwards, each one 1 higher than the previous value, regardless of any FlagsAttributes. (There of course are more elaborate rules for when you specify some values manually – see the MSDN docs)

While it does somewhat make sense to have a flags enumeration automatically get values in powers of two, I see reasons why it's probably best not to do so:

It's perfectly reasonable to have an enum like this:

[Flags]
enum Foo
{
    None,
    Bar,
    Baz, 
    BarAndBaz
} 

This of course requires the values to be 0, 1, 2 and 3 to be sensible.

It'd be pretty counter-intuitive to let the presence of a mere attribute change the entire meaning of a piece of C# code. Consider my Foo example enumeration. Should removing the Flags attribute be allowed to break the code by silently changing the enumerator values?

like image 129
Joren Avatar answered Mar 02 '23 18:03

Joren