Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The plus operator in enum definition

Tags:

c#

enums

I stumbled upon the usage of the plus (+) operator in an enum definition today, I was surprised to see the accompanying tests pass. Anyone have any idea where this may be documented?

public enum ApprovalItemState
{
    Enqueued = 1,
    Approved = 2,
    Denied = 4,
    Acknowledged = 8,
    ApprovalAcknowledged = ApprovalItemState.Approved + ApprovalItemState.Acknowledged,
    DenialAcknowledged = ApprovalItemState.Denied + ApprovalItemState.Acknowledged
}


[TestClass]
public class ApprovalItemStateTests
{
    [TestMethod]
    public void AreFlagsDeniedAndAcknowledged()
    {
        Assert.AreEqual(ApprovalItemState.DenialAcknowledged, ApprovalItemState.Denied | ApprovalItemState.Acknowledged);
    }

    [TestMethod]
    public void IsDenialAcknowledged()
    {
        Assert.IsTrue(Enum.IsDefined(typeof(ApprovalItemState), ApprovalItemState.Denied | ApprovalItemState.Acknowledged));
        Assert.AreEqual(ApprovalItemState.Denied | ApprovalItemState.Acknowledged, (ApprovalItemState)Enum.Parse(typeof(ApprovalItemState), "DenialAcknowledged"));
    }


    [TestMethod]
    public void IsNotDeniedAndApproved()
    {
        Assert.IsFalse(Enum.IsDefined(typeof(ApprovalItemState), ApprovalItemState.Approved | ApprovalItemState.Denied));
    }
}
like image 944
David Avatar asked Feb 15 '12 21:02

David


People also ask

What are the parts of an enum called?

In computer programming, an enumerated type (also called enumeration, enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type.

Where should enums be defined C++?

The best way to define the enum is to declare it in header file. So, that you can use it anywhere you want by including that header file during compilation.

What are enum types?

Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in a number of programming languages. An example of an enum type might be the days of the week, or a set of status values for a piece of data.

What is the meaning of enum?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.


2 Answers

Reed's answer is of course correct. I just thought I'd add an interesting bit of trivia. First off, when you are inside the enum, all the members of the enum are in scope. This is the only situation in C# in which you can use an enum member via its unqualified name!

public enum ApprovalItemState 
{
    Enqueued = 1,
    Approved = 2,
    Denied = 4,
    Acknowledged = 8,
    ApprovalAcknowledged = Approved | Acknowledged,
    DenialAcknowledged =  Denied | Acknowledged 
} 

The second trivia point is that the C# compiler actually allows enum arithmetic involving other enums inside an enum!

enum E
{
    E1
}
enum F
{
    F1
}
enum G
{
    G1 = E.E1 + F.F1
}

Normally that would not be at all legal; you cannot add two dissimilar enums together and you cannot assign the result. The compiler relaxes those rules inside an enum initializer so that you can do things like:

enum MyFlags
{
    MyReadOnly = FileFlags.ReadOnly,
    ...
like image 165
Eric Lippert Avatar answered Nov 15 '22 12:11

Eric Lippert


The C# Language Spec, in 14.5, states:

The following operators can be used on values of enum types: ==, !=, <, >, <=, >= (§7.10.5), binary + (§7.8.4), binary ‑ (§7.8.5), ^, &, | (§7.11.2), ~ (§7.7.4), ++ and -- (§7.6.9 and §7.7.5).

Basically, since the enum is internally stored as an Int32 (that's the default, unless you specify a different storage type), you can use addition like this.

However, it's far more common to use | instead of + to define masks. Also, it would be common to include [Flags] if you're going to use this as a flags enumeration.

like image 34
Reed Copsey Avatar answered Nov 15 '22 10:11

Reed Copsey