Hi I have a bunch of enums in my db class library.
I have a class file where i've listed the enums i use. But enums can be written outside the class and only exist in a namespace. Are there special files in Visual studio to store enums? or do you have to use a cs file?
Hi I have a bunch of enums in my db class library.
I have a class file where i've listed the enums i use. But enums can be written outside the class and only exist in a namespace. Are there special files in Visual studio to store enums? or do you have to use a cs file?
Most would use a class. And there is nothing in VS for enums that I know about.
If you find the post has answered your issue, then please mark post as 'answered'.
An enum is a type and commonly found in a namespace not a class. Since namespaces are used to organize code so you'll find enums that are related to the namespace subject. The same idea applies to classes.
A dedicated class if this is what you meant is likely overkill. It still depends how your library is organized (you identified namespaces that could be used in isolation or together or everything is in a single namespace ?)
If the enum is only used within one class, it should be placed within that class, but if the enum is used between two or more classes, it ought to either be in it's own code file, or in a conglomerated code file that contains all the enum for a particular
assembly.
Here are some related discussions on how to store enums, you could refer to
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
168 Points
201 Posts
Where to store enums?
Dec 05, 2019 08:58 AM|bluMarmalade|LINK
Hi I have a bunch of enums in my db class library.
I have a class file where i've listed the enums i use. But enums can be written outside the class and only exist in a namespace. Are there special files in Visual studio to store enums? or do you have to use a cs file?
Contributor
4923 Points
4198 Posts
Re: Where to store enums?
Dec 05, 2019 03:42 PM|DA924|LINK
Most would use a class. And there is nothing in VS for enums that I know about.
All-Star
53001 Points
23587 Posts
Re: Where to store enums?
Dec 05, 2019 03:55 PM|mgebhard|LINK
An enum is a type and commonly found in a namespace not a class. Since namespaces are used to organize code so you'll find enums that are related to the namespace subject. The same idea applies to classes.
See the C# programming guide.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/enumeration-types
All-Star
48490 Points
18071 Posts
Re: Where to store enums?
Dec 05, 2019 04:00 PM|PatriceSc|LINK
Most often they are declared in the related namespace. See for example https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient?Mosview=netframework-4.8#enums, https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations?view=netframework-4.8#enums and so on...
A dedicated class if this is what you meant is likely overkill. It still depends how your library is organized (you identified namespaces that could be used in isolation or together or everything is in a single namespace ?)
Contributor
2070 Points
606 Posts
Re: Where to store enums?
Dec 06, 2019 02:50 AM|Sherry Chen|LINK
Hi bluMarmalade ,
If the enum is only used within one class, it should be placed within that class, but if the enum is used between two or more classes, it ought to either be in it's own code file, or in a conglomerated code file that contains all the enum for a particular assembly.
Here are some related discussions on how to store enums, you could refer to
https://stackoverflow.com/questions/12386029/separate-class-for-enums
https://stackoverflow.com/questions/2282976/should-enums-in-c-sharp-have-their-own-file
Best Regards ,
Sherry
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.