开发者

C# - Creating a code file with common definitions/constants/enums etc?

开发者 https://www.devze.com 2022-12-13 18:18 出处:网络
In C++ I\'d often create a code file containing constants, enums, #define-s, macros etc. What\'s the best practice for that in C#? Do I create a static class 开发者_Python百科and fill it with that da

In C++ I'd often create a code file containing constants, enums, #define-s, macros etc.

What's the best practice for that in C#? Do I create a static class 开发者_Python百科and fill it with that data? or is there some other way ?


You don't need a static class for enums - they can be top-level (meaning: namespace level). Yes, you need a class for constants (and a static class would suffice), but I would tend to have multiple classes - one per intent. There is no need to cram them all together.

In C#, any #define only apply to that file, so there is not much point having a class for them (put them in the project / build-script instead). And macros don't exist.


If you have some items you want to define Globally, like a set of strings, I would use a static class with Static properties. I would do that if you are going to use it in more than 1 place.

If you are going to use a defined string for example in just once place, then I would put it in the class that is referencing it.

It is very important to use properties and not expose members. I have found with C++ developers I have worked with when they move to C# they expose members because they have no need for "the special logic of a property". While that may be true when you initially are writing the code. If you expose it as a member and need to do special logic then you have to refactor in a major way. While if you begin as a property then you can add the logic with no refactoring.

For Enums I tpyically define an Enum.cs file inside the folder that represents the namespace. Rather than define them inside a static class.


Macros:
Macros don't exist in C#.

#Defines:
defines are very restricted and only really used for conditional compilation. You should define them by using the project properties (or in your msbuild script) instead.

Enums:
Enums should each go in their own separate file. They don't need to be within a class, they just go directly in the name space.

Constants:
Personally I try to keep constants to a minimum, and private within a class where possible.

If you do have to make them public and globally available, use a static class (or a normal class if they relate directly to one nicely). Try to group them into classes by their use.

If you are talking about string constants, you could consider using a resource file instead if they are localizable strings.


Usually there is a class or struct for which your enum etc. particular applies. I put it in that file, under the class. It's easy to get to the definition from anywhere it's used in code. When possible, I try to put all similar entities for a namespace (or other logical grouping) in the same place.


I'd already object to that practice in C++.
Define that stuff where you need it and not in a single "dump" file. This kind of file tends to accumulate huge amounts of unused stuff over time. And it's hard to clean up because who knows, which parts of your code is using it...

0

精彩评论

暂无评论...
验证码 换一张
取 消