Enumerations

  • Thread starter Thread starter C# Learner
  • Start date Start date
C

C# Learner

Imagine the following:

class MyClass
{
public enum ErrorCode
{
InvalidCommand,
NotEnoughParameters,
None
}
//...
}

Should ErrorCode be declared as part of MyClass, as above, or should it
be declared outside of it?
 
I would declare it outside the class. The only time I might create it
inside the class, is when I wrap Win32 API:s and flags (in enums)
to show that it belongs with my interop code. All of the code in my
class is static

public class Win32
{
public enum WindowStyleExtended
{
//....
}

[DllImport("User32.dll")]
public static extern IntPtr CreateWindowEx(WindowStyleExtended e, ....);
}

But I think it's a question of preference. I am however intressted to
know if there are any performance etc differences. And how does
the resuling MSIL differ when it's declared inside the class. Perhaps
someone with a bit more experience digging around this kind of stuff
could fill in the blanks ? =)

//Andreas
 
Andreas said:
I would declare it outside the class. The only time I might create it
inside the class, is when I wrap Win32 API:s and flags (in enums)
to show that it belongs with my interop code. All of the code in my
class is static

[...]

Thanks Andreas. I'm still considering both options.
 
I think it depends: if "enum ErrorCode" is only used by "MyClass", it is ok
to define it inside MyClass. People reading your code will understand right
away that this enum is only used inside MyClass, not anywhere else. This is
more true if the file contains lots of classes. But if the enum is also used
by other classes, it of cause has to be defined outside MyClass.
 
Back
Top