Галерея 2282976

Галерея 2282976




🛑 ПОДРОБНЕЕ ЖМИТЕ ЗДЕСЬ 👈🏻👈🏻👈🏻

































Галерея 2282976

Sign up or log in to customize your list.

more stack exchange communities

company blog


Stack Overflow for Teams
– Start collaborating and sharing organizational knowledge.



Create a free Team
Why Teams?



Modified
4 years, 10 months ago


Closed . This question is opinion-based . It is not currently accepting answers.

15.5k 9 9 gold badges 55 55 silver badges 89 89 bronze badges



Sorted by:


Reset to default





Highest score (default)


Trending (recent votes count more)


Date modified (newest first)


Date created (oldest first)




101k 36 36 gold badges 181 181 silver badges 257 257 bronze badges


155k 29 29 gold badges 288 288 silver badges 343 343 bronze badges


44.6k 16 16 gold badges 81 81 silver badges 88 88 bronze badges


7,871 3 3 gold badges 25 25 silver badges 42 42 bronze badges


19k 4 4 gold badges 44 44 silver badges 60 60 bronze badges


5,525 7 7 gold badges 48 48 silver badges 54 54 bronze badges


12.2k 8 8 gold badges 57 57 silver badges 92 92 bronze badges


376 1 1 silver badge 5 5 bronze badges


125k 54 54 gold badges 294 294 silver badges 443 443 bronze badges


2,518 2 2 gold badges 16 16 silver badges 22 22 bronze badges


61 1 1 silver badge 1 1 bronze badge


2,278 6 6 gold badges 19 19 silver badges 35 35 bronze badges


990 1 1 gold badge 9 9 silver badges 18 18 bronze badges


517 7 7 silver badges 14 14 bronze badges


9,735 3 3 gold badges 29 29 silver badges 44 44 bronze badges


Not the answer you're looking for? Browse other questions tagged c# coding-style enums or ask your own question .

Stack Overflow

Questions
Help



Products

Teams
Advertising
Collectives
Talent



Company

About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us
Cookie Settings
Cookie Policy



Stack Exchange Network



Technology




Culture & recreation




Life & arts




Science




Professional




Business





API





Data






Accept all cookies



Necessary cookies only


Find centralized, trusted content and collaborate around the technologies you use most.
Connect and share knowledge within a single location that is structured and easy to search.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post .
I have a class which uses an enumeration, the enum is currently in its own file which seems wasteful.
What is the general opinion on enums being placed within the namespace of a file that they are consumed in? Or should the enum really live in its own cs file?
I should mention that while the class in question uses these enumerations, so does external callers. In other words, another class can set these enumerations. So they are not used internally to the class, otherwise this question would be a no brainer.
I wouldn't say "wasteful" (how much does an extra file cost?), but it is often inconventient. Usually there's one class that's most closely associtated with the enum, and I put them in the same file.
This is really just a matter of preference.
I prefer to put each enumeration in its own file (likewise for each interface, class, and struct, no matter how small). It makes them easier to find when I'm coming from another solution or otherwise don't already have a reference to the type in question.
Putting a single type in each file also makes it easier to identify changes in source control systems without diffing.
This is entirely a matter of style. What I tend to do is to have a file called Enums.cs in the solution in which the enum declarations are collected.
But they are typically found through the F12 key anyway.
The question to ask yourself would be: is there anything about an enumeration type in C# that indicates I should treat it differently from all other types I create?
If the enumeration is public, it should be treated like any other public type. If it is private, declare it as a nested member of the class using it. There is no compelling reason to put two public types in the same file simply because one is an enumeration. The fact that it is a public type is all that matters; the flavor of type does not.
Another advantage of putting each type (class, struct, enum) in its own file is source control. You can easily get the entire history of the type.
I place mostly inside in namespace and outside of class so that it is easily accessible other classes in that namespace like below.
Generally I prefer my enums to be in the same file as the Class that it will most probably be an attribute of. If for example I have a class Task then the enum TaskStatus will be in the same file.
However, if I have enums of a more generic nature, then I keep them contextually in various files.
It depends on what access is needed.
If the enum is only used by a single class, it's okay to declare it within that class because you don't need to use it anywhere else.
For enums used by multiple classes or in a public API, then I will always keep the definition in its own file in the appropriate namespace. It's far easier to find that way, and the strategy follows the pattern of one-object-per-file, which is good to use with classes and interfaces as well.
I think that depends on the scope of the enum. For example if the enum is specific to one class, for example used to avoid the magic constant scenario, then I would say put it in the same file as the class:
If the enum is general and can be used by several classes for different scenarios, then I would be inclined to use put it in its own file. For example the below could be used for several purposes:
I tend to put enums in their own file for a very simple reason: as with classes and structs, it's nice to know exactly where to look if you want to find a type's definition: in the file of the same name. (To be fair, in VS you can always use "Go to Definition," too.)
Obviously, it can get out of hand. A colleague where I work even makes separate files for delegates.
One advantage of using a separate file for enums is that you can delete the original class that used the enum and write a new class using the enum.
If the enum is independent of the original class then putting it in a separate file makes future changes easier.
If you are using the USysWare File Browser add-in for Visual Studio, you can very quickly find files of particular names in your solution. Imagine looking for an enum that is not in its own file but instead buried in some file in a gigantic solution.
For small solutions, it doesn't matter, but for large ones, it becomes all the more important to keep classes and enums in their own files. You can quickly find them, edit them, and more. I highly, highly recommend putting your enum in its own file.
And as was stated... How wasteful is a file that ends up only being a couple of kb anyways?
Very simple huge advantage to separate file. When any object is in its own MyObjectName.cs file... you can go to solution explorer and type MyObjectName.cs and be shown exactly 1 file. Anything that makes debugging better is nice.
Another advantage on a similar note, if you search all files ( ctrl + shft + F ) for a name, you may find 20 references to the name in the same file... and that found name will be part of different objects. In the Find Results window all you can see is the line number and the file name. You would have to open the file and scroll to figure out which object the found reference was in.
Anything that makes debugging easier, I like.
If you have multiple projects in one solution. Then better create another project Utilities . Then create a Folder \Enumerations and create a nested static class . And then assign each static class where you will create enum that corresponds to the name of your projects. For example you have a project named DatabaseReader and DatabaseUsers then you may name the static class like
Then entire enum that can be used in the entire solutions per projects will be declared on it. Use # region to separate each concern. By this, it is easier to look for any enums
I like to have one public enums file named E containing each seperate enum, then any enum can be accessed with E... and they are in one place to manage.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.9.43293


By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .



Sign up or log in to customize your list.

more stack exchange communities

company blog


Stack Overflow for Teams
– Start collaborating and sharing organizational knowledge.



Create a free Team
Why Teams?



Modified
4 years, 10 months ago


Closed . This question is opinion-based . It is not currently accepting answers.

15.5k 9 9 gold badges 55 55 silver badges 89 89 bronze badges



Sorted by:


Reset to default





Highest score (default)


Trending (recent votes count more)


Date modified (newest first)


Date created (oldest first)




101k 36 36 gold badges 181 181 silver badges 257 257 bronze badges


155k 29 29 gold badges 288 288 silver badges 343 343 bronze badges


44.6k 16 16 gold badges 81 81 silver badges 88 88 bronze badges


7,871 3 3 gold badges 25 25 silver badges 42 42 bronze badges


19k 4 4 gold badges 44 44 silver badges 60 60 bronze badges


5,525 7 7 gold badges 48 48 silver badges 54 54 bronze badges


12.2k 8 8 gold badges 57 57 silver badges 92 92 bronze badges


376 1 1 silver badge 5 5 bronze badges


125k 54 54 gold badges 294 294 silver badges 443 443 bronze badges


2,518 2 2 gold badges 16 16 silver badges 22 22 bronze badges


61 1 1 silver badge 1 1 bronze badge


2,278 6 6 gold badges 19 19 silver badges 35 35 bronze badges


990 1 1 gold badge 9 9 silver badges 18 18 bronze badges


517 7 7 silver badges 14 14 bronze badges


9,735 3 3 gold badges 29 29 silver badges 44 44 bronze badges


Not the answer you're looking for? Browse other questions tagged c# coding-style enums or ask your own question .

Stack Overflow

Questions
Help



Products

Teams
Advertising
Collectives
Talent



Company

About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us
Cookie Settings
Cookie Policy



Stack Exchange Network



Technology




Culture & recreation




Life & arts




Science




Professional




Business





API





Data






Accept all cookies



Necessary cookies only


Find centralized, trusted content and collaborate around the technologies you use most.
Connect and share knowledge within a single location that is structured and easy to search.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post .
I have a class which uses an enumeration, the enum is currently in its own file which seems wasteful.
What is the general opinion on enums being placed within the namespace of a file that they are consumed in? Or should the enum really live in its own cs file?
I should mention that while the class in question uses these enumerations, so does external callers. In other words, another class can set these enumerations. So they are not used internally to the class, otherwise this question would be a no brainer.
I wouldn't say "wasteful" (how much does an extra file cost?), but it is often inconventient. Usually there's one class that's most closely associtated with the enum, and I put them in the same file.
This is really just a matter of preference.
I prefer to put each enumeration in its own file (likewise for each interface, class, and struct, no matter how small). It makes them easier to find when I'm coming from another solution or otherwise don't already have a reference to the type in question.
Putting a single type in each file also makes it easier to identify changes in source control systems without diffing.
This is entirely a matter of style. What I tend to do is to have a file called Enums.cs in the solution in which the enum declarations are collected.
But they are typically found through the F12 key anyway.
The question to ask yourself would be: is there anything about an enumeration type in C# that indicates I should treat it differently from all other types I create?
If the enumeration is public, it should be treated like any other public type. If it is private, declare it as a nested member of the class using it. There is no compelling reason to put two public types in the same file simply because one is an enumeration. The fact that it is a public type is all that matters; the flavor of type does not.
Another advantage of putting each type (class, struct, enum) in its own file is source control. You can easily get the entire history of the type.
I place mostly inside in namespace and outside of class so that it is easily accessible other classes in that namespace like below.
Generally I prefer my enums to be in the same file as the Class that it will most probably be an attribute of. If for example I have a class Task then the enum TaskStatus will be in the same file.
However
Нудизм на пляже
Черные члены в сперме
Женщины с разным сроком беременности демонстрируют свои тела – они тоже хотят быть сексуальными

Report Page