Do .net FlagsAttribute enums need to be manually valued?

故事扮演 提交于 2021-02-05 05:49:11

问题


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?


回答1:


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?




回答2:


Yes, you have to specify the values in the Enum to get the added flag values to work correctly (to be able to combine two enum flags and not have that value conflict with another flag specified). You don't have to specify values to get it to compile, but the outcome will most likely not be the one you are looking for.



来源:https://stackoverflow.com/questions/1607442/do-net-flagsattribute-enums-need-to-be-manually-valued

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!