350

I'm asking this question despite having read similar but not exactly what I want at C# naming convention for enum and matching property

I found I have a tendency to name enums in plural and then 'use' them as singular, example:

public enum EntityTypes {
  Type1, Type2
}

public class SomeClass {
  /*
    some codes
  */

  public EntityTypes EntityType {get; set;}

}

Of course it works and this is my style, but can anyone find potential problem with such convention? I do have an "ugly" naming with the word "Status" though:

public enum OrderStatuses {
  Pending, Fulfilled, Error, Blah, Blah
}

public class SomeClass {
  /*
    some codes
  */

  public OrderStatuses OrderStatus {get; set;}

}

Additional Info: Maybe my question wasn't clear enough. I often have to think hard when naming the variables of the my defined enum types. I know the best practice, but it doesn't help to ease my job of naming those variables.

I can't possibly expose all my enum properties (say "Status") as "MyStatus".

My question: Can anyone find potential problem with my convention described above? It is NOT about best practice.

Question rephrase:

Well, I guess I should ask the question this way: Can someone come out a good generic way of naming the enum type such that when used, the naming of the enum 'instance' will be pretty straightforward?

Community
  • 1
  • 1
o.k.w
  • 25,490
  • 6
  • 66
  • 63
  • 6
    public enum OrderState... - public OrderState OrderStatus { get; set;} – Fraser Mar 07 '11 at 22:33
  • As per other comments and answers, we'd better not use plural for enum names. This almost inevitably leads to headaches when giving a name to the corresponding field, and I have no solution for this. I use plural for arrays and collections, and in that case I disregard the common English syntax, by just appending an 's'. So a collection of elements representing a status will be called statuss. ;-) – Giorgio Barchiesi Dec 30 '22 at 14:51
  • Generally speaking [System Hungarian Notation](https://en.wikipedia.org/wiki/Hungarian_notation) is frowned upon. This is a general comment to prefixing/suffixing an Enum name with some identifier of it's data type, generally bad practice and should be avoided. – Erik Philips Mar 08 '23 at 16:00

10 Answers10

424

Microsoft recommends using singular for Enums unless the Enum represents bit fields (use the FlagsAttribute as well). See Enumeration Type Naming Conventions (a subset of Microsoft's Naming Guidelines).

To respond to your clarification, I see nothing wrong with either of the following:

public enum OrderStatus { Pending, Fulfilled, Error };

public class SomeClass { 
    public OrderStatus OrderStatus { get; set; }
}

or

public enum OrderStatus { Pending, Fulfilled, Error };

public class SomeClass {
    public OrderStatus Status { get; set; }
}
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
jason
  • 236,483
  • 35
  • 423
  • 525
  • 28
    Yes, this is a correct answer. This guidlines are used in the .Net Framework e.g. enum DayOfWeek and flags enum RegexOptions. – Alexander Zwitbaum Sep 10 '09 at 15:37
  • 3
    Yes, this is the recommended practice, I welcome it. However it does not answer my question. – o.k.w Sep 10 '09 at 15:41
  • 1
    @o.k.w to further elaborate, although it looks ugly, if you need a single value from a flag enum use the singular form for the field/property/argument. If you support it having multiple flags set, use the plural. If your enum is not a flags enum, use the singular for the type name and the field/property/arguments. – Jonathan Dickinson Oct 03 '11 at 13:42
  • 4
    Here is [the link to the .Net 4.0 version](http://msdn.microsoft.com/en-us/library/ms229040.aspx) of the Microsoft naming conventions guide linked to in the answer. –  Mar 22 '12 at 18:24
  • 1
    As a question to this old answer.....does public OrderStatus OrderStatus REALLY work? (never seen a typename equal to a variable name) – Thomas Jan 17 '17 at 12:44
  • 2
    @Thomas I've never had a problem with it, I can't see why it wouldn't work--don't see a context where it would be ambiguous whether it's a type or variable being referenced. i.e. `OrderStatus == OrderStatus.Pending` is recognized as a variable for the left and then an enumeration on the right – James Hurley Jan 31 '17 at 23:04
  • @Thomas On MSDN, from [Names of Type Members](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members) in the section "Names of Properties": **✓ CONSIDER** giving a property the same name as its type. **Example:** `public Color Color { get {...} set {...} } ` – DavidRR Jul 20 '17 at 17:59
49

I started out naming enums in the plural but have since changed to singular. Just seems to make more sense in the context of where they're used.

enum Status { Unknown = 0, Incomplete, Ready }

Status myStatus = Status.Ready;

Compare to:

Statuses myStatus = Statuses.Ready;

I find the singular form to sound more natural in context. We are in agreement that when declaring the enum, which happens in one place, we're thinking "this is a group of whatevers", but when using it, presumably in many places, that we're thinking "this is one whatever".

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
  • 8
    A little late reaction (and maybe a little off-topic) but: i would suggest using value `0` for the unknown value, that way an uninitialized variable is by default `Unknown`. – SvenL Jan 27 '16 at 06:49
  • 1
    Agreed, @SvenL. Updated example accordingly. – Bob Kaufman Nov 14 '16 at 15:29
32

The situation never really applies to plural.

An enum shows an attribute of something or another. I'll give an example:

enum Humour
{
  Irony,
  Sarcasm,
  Slapstick,
  Nothing
}

You can have one type, but try think of it in the multiple, rather than plural:

Humour.Irony | Humour.Sarcasm

Rather than

Humours { Irony, Sarcasm }

You have a sense of humour, you don't have a sense of humours.

Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
  • 12
    Haha, well, programmers are not always grammatically/politically correct. In your case, I probable use "HumourTypes". Bad habit I guess. – o.k.w Sep 10 '09 at 15:23
  • 1
    What if I want to search for all individuals who have a Sense of Sarcasm OR have a sense of irony, would I not pass the search routine an instance of `Humours` containing `Humours.Irony | Huomours.Sarcasm` ?? – Charles Bretana Sep 13 '14 at 17:24
18

This is one of the few places that I disagree with the convention enough to go against it. TBH, I HATE that the definition of an enum and the instance of it can have the same name. I postfix all of my Enums with "Enum" specifically because it makes it clear what the context of it is in any given usage. IMO it makes the code much more readable.

public enum PersonTypesEnum {
    smart,
    sad,
    funny,
    angry
}


public class Person {   
    public PersonTypesEnum PersonType {get; set;}
}

Nobody will ever confuse what is the enum and what is the instance of it.

Heather
  • 2,602
  • 1
  • 24
  • 33
  • 4
    I came here looking for a enum naming convention, after having a class and enum named the same - and wanted to have "something" to make it more obvious. I was thinking prefixing it with "E" (for Enums obviously) like we prefix interfaces with "I" - but I liked your solution Heather! Nice one!!! – Scott Jun 13 '19 at 08:55
  • 8
    From Microsoft's design guilelines: "DO NOT use an "Enum" suffix in enum type names." https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-classes-structs-and-interfaces – Thoryn Hawley Jun 20 '19 at 18:11
  • 12
    Perhaps you missed the VERY FIRST sentence of what I said? Here, let me copy and paste it for you: "This is one of the few places that I disagree with the convention enough to go against it.". I then go on to explain why. – Heather Jun 21 '19 at 20:56
  • 1
    I understand that you are going against guidelines here in every possible way. But this just put bad example out there for people. Especially in the sense of how to name anything at all in a program. like we should not be naming List with suffix list and instead, use proper names for what entity it actually represents. Everything including Enum should be named according to what entity it represents. – Rohin Tak Aug 29 '19 at 13:36
  • 9
    I'm not going against the guidelines "in every possible way". That's hyperbole. I'm going against the guidelines in a single, specific way, which is supported by the reasoning I state. If you want to disagree, fine, list your reasons for disagreeing; your hyperbole is unnecessary and does not advance your position. – Heather Sep 16 '19 at 23:22
  • 5
    If there is a namespace collision possible, I see no issue with adding `Enum`? It's not as if the author is proposing postfixing all vars with their type. Author also has a much stronger case given that a reason is provided, whereas M$ provides zero justification. – Jai Govindani Nov 18 '19 at 20:15
17

In general, the best practice recommendation is singular, except for those enums that have the [Flags] attribute attached to them, (and which therefore can contain bit fields), which should be plural.

After reading your edited question, I get the feeling you may think the property name or variable name has to be different from the enum type name... It doesn't. The following is perfectly fine...

  public enum Status { New, Edited, Approved, Cancelled, Closed }

  public class Order
  {
      private Status stat;
      public Status Status
      { 
         get { return stat; }
         set { stat = value; }
      }
  }
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • True, I guess my method is a 'quick and lazy' way of avoiding the need to think of names when using the enums. – o.k.w Sep 10 '09 at 15:10
  • 1
    In support of your answer: on MSDN, from [Names of Type Members](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members) in the section "Names of Properties": **✓ CONSIDER** giving a property the same name as its type. **Example:** `public Color Color { get {...} set {...} } ` – DavidRR Jul 20 '17 at 18:07
8

If you are trying to write straightforward, yet forbidden code like this:

    public class Person
    {
        public enum Gender
        {
            Male,
            Female
        }
        //Won't compile: auto-property has same name as enum
        public Gender Gender { get; set; }  
    }

Your options are:

  1. Ignore the MS recommendation and use a prefix or suffix on the enum name:

    public class Person
    {
        public enum GenderEnum
        {
            Male,
            Female
        }
        public GenderEnum Gender { get; set; }
    }
    
  2. Move the enum definition outside the class, preferably into another class. Here is an easy solution to the above:

    public class Characteristics
    {
        public enum Gender
        {
            Male,
            Female
        }
    }
    public class Person
    {
        public Characteristics.Gender Gender { get; set; }  
    }
    
MiloNC
  • 445
  • 5
  • 7
  • 2
    Hypothetical situation and not a good solution. Why use a nested `enum` in the first place and then nest it in yet another class if this causes trouble? – Gert Arnold Aug 06 '12 at 22:00
  • 1
    In case of Gender, its much more meaningful to have property name as `Gender` and enum name as `Sex`. So `isac.Gender = Sex.Male`.. – nawfal Mar 29 '13 at 12:11
  • 5
    I'm not sure why this guy is being downvoted. This situation is legitimate and is far from hypothetical. One nests enum types in C# for similar reasons that one might use an inner class in Java... because the inner type is used only in the outer and nowhere else, and makes sense only in the context of the outer and not elsewhere. And as a result of compiler limitations, you do have to choose one of the solutions mentioned. – asdfjklqwer Aug 14 '13 at 00:56
  • You will have to set it from somewhere, usually outside the class, or maybe when constructing the class, in which case you need the enum to be defined outside, unless you want to send in Person.Gender.Male, Gender could apply to more than just people, i think not having it nested is the best solution anyway. – Jim Wolff Sep 11 '13 at 10:52
  • 2
    Another, possibly better option is the answer from "Serge - appTranslator". – Curtis Yallop May 22 '14 at 18:43
7

Best Practice - use singular. You have a list of items that make up an Enum. Using an item in the list sounds strange when you say Versions.1_0. It makes more sense to say Version.1_0 since there is only one 1_0 Version.

Jeremy Cron
  • 2,404
  • 3
  • 25
  • 30
7

Coming in a bit late...

There's an important difference between your question and the one you mention (which I asked ;-):

You put the enum definition out of the class, which allows you to have the same name for the enum and the property:

public enum EntityType { 
  Type1, Type2 
} 

public class SomeClass { 
  public EntityType EntityType {get; set;} // This is legal

}

In this case, I'd follow the MS guidelins and use a singular name for the enum (plural for flags). It's probaby the easiest solution.

My problem (in the other question) is when the enum is defined in the scope of the class, preventing the use of a property named exactly after the enum.

Community
  • 1
  • 1
Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
1

The reason for using a plural for a enum declaration is the fact that ( at the time of declaration ) we declare it with multiple values, so plural seems good... But we ignore the fact that enum when declared specifies what value it can have ( from the given set of values ). It doesn't mean that the instance of that enum will store multiple values..... When we write: enum Days { MON, TUE, WED, THU, FRI, SAT, SUN}; We are making it plural because of the multiple values provide.. However when used (Days day = Days.MON; ) we completely ignore that instance of that enum is supposed to have a single value.... So when we write : enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }; We mean that there is a enum that can have any one day as its value, so singular is more appropriate. Although (already described above ), to get around this without using singular names is by using any kind of Indicator, like DayEnum or EDay ( i prefer the second one)....

0

On the other thread C# naming convention for enum and matching property someone pointed out what I think is a very good idea:

"I know my suggestion goes against the .NET Naming conventions, but I personally prefix enums with 'E' and enum flags with 'F' (similar to how we prefix Interfaces with 'I')."

Community
  • 1
  • 1
RenniePet
  • 11,420
  • 7
  • 80
  • 106