views:

42

answers:

2

As I know I can define a new Enum DataFormat at the my project Class, Suppose, I don't do that, I can reference the DataFormat from the 3rd-Party DLL. Then I can use the Enum DataFormat from the metadata of 3rd-Party DLL too.

What's the difference? And are the any guideline to define A new Enum DataFormat ? Thanks.

    public enum DataFormat
    {
        SECS = 0,
        S2_L = 4,
        S2_B = 8,
        S2_BOOLEAN = 12,
        S2_A = 16,
        S2_J = 20,
        S2_U1 = 24,
        S2_U2 = 25,
        S2_U4 = 26,
        S2_U8 = 27,
        S2_I1 = 28,
        S2_I2 = 29,
        S2_I4 = 30,
        S2_I8 = 31,
        S2_F4 = 34,
        S2_F8 = 35,
        S2_STRING = 36,
    }
}
+1  A: 

The general rule that I like to follow is that you define classes, enumerations, methods and variables with the smallest scope required for them to work. So if the enumeration is only accessed from a single class, then define it as a member of that class. If it's accessed outside of the class, the define it outside of the class.

Dean Harding
@codeka, With your rule, If I want to access outside the class. Then I may have two choices: 1. Outside class; 2.Interface metadata. Which one is better? thank you.
Nano HE
+1  A: 

There's no "metadata" here - Enum types are still types. If one already exists in a separate assembly, then the only reason you would want to recreate it in another project is if there's some specific reason why you cannot reference the original assembly.

If you have two (identical or almost identical) versions of the same type - any type - in any solution, where "solution" includes referenced assemblies, then you are running a very real risk of conflicts or at least confusion, for no tangible benefit that I can see.

If the type doesn't exist yet and you are asking where it should be created... normally a type lives where its dependencies begin. If it's just going to be sitting in the external assembly doing nothing, then it shouldn't be there. On the other hand, if other classes in the external assembly depend on it, then it definitely needs to go in there or in one of the external assembly's dependencies, otherwise you'll probably end up with an ugly circular namespace dependency that you'll need to eliminate (and it can be very difficult to do later in the game).

So, basically: Don't define your own types identical to a type that already exists, unless you have a very good reason, and don't define any type in a project/assembly where you don't actually intend to use it.

Aaronaught
Thank you so much.
Nano HE