74

Hi I'm trying to declar a static enum like so:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Lds.CM.MyApp.Controllers
{
    public class MenuBarsController : Controller
    {
        // Menu Bar enums
        public static enum ProfileMenuBarTab { MainProfile, Edit, photoGallery }

        public ActionResult cpTopMenuBar(string tabSelected)
        {
            ...            

" But I'm getting the following error: "The modifier 'static' is not valid for this item." I know it's something simple but I can't seem to see the problem. Much thanks!

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
RayLoveless
  • 19,880
  • 21
  • 76
  • 94
  • Just the same for [delegates](http://stackoverflow.com/questions/6835766/why-can-a-net-delegate-not-be-declared-static) and [structs](http://stackoverflow.com/questions/2613376/why-cant-you-declare-a-static-struct-in-c-but-they-can-have-static-methods) – nawfal Jan 02 '14 at 17:05

6 Answers6

161

Enums are types, not variables. Therefore they are 'static' per definition, you dont need the keyword.

public enum ProfileMenuBarTab { MainProfile, Edit, PhotoGallery }
magnattic
  • 12,638
  • 13
  • 62
  • 115
  • 1
    enums are 'static' by definition but we can't use it inside static class why? – user2323308 Feb 01 '17 at 06:03
  • @user2323308 **10.1.1.3 Static classes** A static class cannot be instantiated, cannot be used as a type and can contain only static members... A static class can only contain static members (**§10.3.7**). *Note that constants and nested types are classified as static members*. **10.3.7 Static and instance members** Members of a class are either static members or instance members... *...a constant or type declaration implicitly declares a static member.* ref: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification – samus Jun 19 '17 at 16:12
17

Take out static.
Enums are types, not members; there is no concept of a static or non-static enum.

You may be trying to make a static field of your type, but that has nothing to do with the type declaration.
(Although you probably shouldn't be making a static field)

Also, you should not make public nested types.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thanks, it's been a long night. I removed the "static" and all is well. Also I was reading your link and it appears that a nested enum is an exception to your rule. "Nested enumerators and protected types are exempt from this rule." – RayLoveless Dec 31 '10 at 03:53
  • @SLaks, does this rule exception make sense to you? – RayLoveless Dec 31 '10 at 04:29
  • @Ray: An enumerator (`IEnumerator` implementation) has nothing to do with an enum. – SLaks Dec 31 '10 at 13:47
  • "Nested enumerators and protected types are exempt from this rule." – Aaron H. Feb 24 '11 at 04:54
  • @SLaks, He's got a public enum, you posted a link that says not to make public nested types, and I was just pointing out that according to the page you linked enums are exempt, so valid to make public. – Aaron H. Feb 24 '11 at 16:31
  • @Aaron: An enumerator (`IEnumerator` implementation) has nothing to do with an enum. – SLaks Feb 24 '11 at 16:34
  • @SLaks, I guess it might be helpful if I read the comments before mine... Sorry for the repeat. But somethings still not clear; you keep saying that IEnumerator has nothing to do with an enum, but why are you saying that? I don't see any relationship to his enum question and an interface for enumerating collections? – Aaron H. Feb 24 '11 at 17:00
  • @Aaron: The MSDN guideline that you and Ray are quoting is talking about `IEnumerator`. (That what "Nested enumerators" means) – SLaks Feb 24 '11 at 17:20
  • @SLaks, got it, I didn't recognize the distinction "enumeration" vs "enumerator" when reading it. That's a confusing rule exception, though makes sense after thinking about it for a while. Thanks. – Aaron H. Feb 24 '11 at 17:47
  • @Aaron: It's used by all of the builtin generic collections (with structs, for some reason) – SLaks Feb 24 '11 at 17:53
  • 1
    At some point in the intervening decade, the text in your original link must have been updated. It now reads "Nested _enumerations_ and protected types are exempt from this rule." (emphasis mine) – Art Schmidt Apr 30 '20 at 16:46
8

You don't need to define it as static.When an enumerated type is compiled, the C# compiler turns each symbol into a constant field of the type . For example, the compiler treats the Color enumeration shown earlier as if you had written code similar to the following:

internal struct Color : System.Enum {
            // Below are public constants defining Color's symbols and values
            public const Color White  = (Color) 0;
            public const Color Red    = (Color) 1;
            public const Color Green  = (Color) 2;
            public const Color Blue   = (Color) 3;
            public const Color Orange = (Color) 4;
            // Below is a public instance field containing a Color variable's value
            // You cannot write code that references this instance field directly
            public Int32 value__;
}
Tarik
  • 79,711
  • 83
  • 236
  • 349
2

An enum is a type, not a value. The modifier static doesn't make much sense there.

Brian Clapper
  • 25,705
  • 7
  • 65
  • 65
  • 4
    A class is a type too and can be static. and since you cannot access this enum with an instance of that class, it sure is static and therefore should use the static modifier, everything else is just inconsistent, but then again it's C#.. – MushyPeas Jan 16 '15 at 11:45
1

You are trying to make an enum declartion static, ie a field of the type ProfileMenuBarTab. To declare a class (or whatever) in a class, leave the static out.

Femaref
  • 60,705
  • 7
  • 138
  • 176
1

Types in .Net can be either value types or reference types.

Value types -> enums, structs and built-in values types(bool, byte, short, int, long, sbyte, ushort, uint, ulong, char, double, decimal)

Reference types -> classes, interfaces, delegates, dynamic and strings

So, as you can see enums are types(like classes and structs, etc). more precisely they are value types. An important point about value types is that you should be able to create instances from them. For example, What is its benefit of int that is a struct (value type) if you can't create an instance of that for storing 2, 3 or any number in it?!

This is the general rule -> you cannot create custom value types (enums and structs) with the static modifier.

Some points:

  • If you write your enums or structs directly in a namespace they can not be marked as private or protected just like other types. They can be just public or internal just like other types.

  • If you write your enums or structs directly in a class you can mark them as private or protected too, as you can mark them as internal and public. class for inner types is like a namespace for types except you can mark inner types private or public too.

halfer
  • 19,824
  • 17
  • 99
  • 186