Question

While brushing up on enums, I ran across the following at MSDN Developer Network's site.

Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.

This makes sense as I need all of my enums to be available to all of the classes in my project. However, I can't seem to find any examples. Here's what I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myNamespace
{
    enum foo {A, B, C, D}
    enum foo2 {E, F, G, H}

    class Myclass
    {
          magicHappens();
    }
}

I am wondering if I need to include the enum in EVERY file I include in this namespace, or if defining it in only one file will pull those enum definitions into the project as a whole at compile time? And is there a specific class it needs to be in? Or is it best practice to just create a file that does nothing but define namespace-level enums?

Any tips would be appreciated.

Was it helpful?

Solution 3

Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.

You seem to misunderstand the above statement. It says that define the enum in namespace level not inside the scope of another type.

Do this:

namespace myNamespace
{
    enum foo {A, B, C, D} //Note enum is outside the class 

    class Myclass
    {
        magicHappens();
    }
}

Don't do this:

namespace myNamespace
{
    class Myclass
    {
        magicHappens();

        enum foo //Note enum is inside the class
        {
           A,
           B,
           C,
           D
        }
    }
}

and Ideally you should have seperate ".cs" files for your enums in this case "foo.cs".

OTHER TIPS

Define each of your enumerations in their own source files (e.g. foo.cs). As long as the namespace is the same of your classes, it will be available for use in your other code files.

Foo.cs:

namespace myNamespace
{
    public enum foo
    {
        A = 0,
        B,
        C,
        D,
    }
}

Foo2.cs:

namespace myNamespace
{
    public enum foo2
    {
        E = 0,
        F,
        G,
        H,
    }
}

Etc.

You say you are creating the enums in a class, but you aren't. This is what an enum in a class looks like:

namespace myNamespace
{

    class Myclass
    {
        enum foo {A, B, C, D}
        enum foo2 {E, F, G, H}
        magicHappens();
    }
}

I think you may be confusing a class definition with a file. You can put as many classes or structs or enums as you want in a file.

You can define your enum with public access modifier, so in other assemblies you can access them, by the way you need to refer their namespace by using, if you want to make them visible with out using their namespace you can define your enum in for example System namespace, as System in referenced in all classes by default then you don't need to add nay using statement, p.s. this method is not recommended.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top