英文:
Enum type within static class
问题
1: 为什么在静态类内部的公共枚举需要在使用时声明其命名空间,尽管已经使用 "using" 导入了它?
2: 在静态类中使用公共枚举是否有任何原因?
英文:
I ran into this legacy code that has static class that contains an enum and extension method with its own namespace. I have simplied it to below:
namespace AuxValidator
{
public static class AuxValidator
{
public enum BadDataSentinal { UPPER = 0, LOWER = 1, WHATEVER = 2 }
public static string Upper(this string field, BadDataSentinal badData, List<string> NewDataWatchlist = null)
{
if(badData == BadDataSentinal.UPPER)
return field.Upper();
return field
}
}
}
This above class sits inside App_Code.
It is used in one of my aspx.cs file,
using AuxValidator;
public partial class SimplePage : RootPage
{
private void updateBoxes(object source, ServerValidateEventArgs args, string controlName, ValidationFunction f)
{
CustomValidator cv = (CustomValidator)source;
TextBox src = ((TextBox)cv.FindControl(controlName));
src.Text = args.Value.Upper(AuxValidator.AuxValidator.BadDataSentinal.UPPER);
}
}
This works. However, notice in order to access the Enum, it has to first declare the namespace AuxValidator, then the class AuxValidator just to use the enum. If I take out the namespace and try to use it like AuxValidator.BadDataSentinal.UPPER
, I will get:
> The type or namespace name 'BadDataSentinal' does not exist in the
> namespace 'AuxValidator'
It is a bit awkward, so intuitively, I take BadDataSentinal
out of the class AuxValidator so I can access it simply with BadDataSentinal.UPPER
.
Or,I can get rid of the namespace inside my AuxValidator
class, so I can access like AuxValidator.BadDataSentinal.UPPER
.
My question:
1: Why does a public enum inside static class needs to declare the namespace where it is used, despite already imported it with "using"
2: Is there any reason for public enum in static class?
答案1
得分: 3
1: 静态类内部的公共枚举为什么需要声明所使用的命名空间,尽管已经使用 "using" 导入了它?
这是因为命名空间和类的名称都相同,即 AuxValidator
,因此编译器将AuxValidator解释为命名空间。如果它们有不同的名称,您就不需要两次指定命名空间。
2: 静态类中的公共枚举是否有任何特定原因?
实际上没有。
英文:
> 1: Why does a public enum inside static class needs to declare the
> namespace where it is used, despite already imported it with "using"
This is because the namespace and the class share the same name AuxValidator
therefore the compiler will interpret AuxValidator as namespace. If they have different name you will not need to specifie the namespace twice
> 2: Is there any reason for public enum in static class?
Not really.
答案2
得分: 2
以下是翻译好的部分:
"Why does a public enum inside static class needs to declare the namespace where it is used, despite already imported it with "using"" - 一个良好的编码标准会禁止命名空间和类具有相同的名称。这里有一个很好的例子说明为什么应该这样做——这样你就不需要使用完全限定的名称,从而提高代码的清晰度。
"Is there any reason for public enum in static class?" - 根据良好的标准,只有在这些枚举仅由这些类使用,而不在其他地方使用时,可以在静态或实例类中声明公共枚举。
"Or, I can get rid of the namespace" - 你应该将命名空间重命名为与你的类不同的名称。此外,类和命名空间应该具有不同的命名风格。如果你的类以正确的风格命名为 AuxValidator
,那么命名空间应该是类似 SomeValidation
的名称。由于这是在 APP_Code
中,你应该没有问题进行重命名,因为这不像是分发库,你不希望通过破坏性更改来让客户感到不满。
英文:
All what you have is available to broad interpretation. But if you apply certain rules, you can standardize your code
"Why does a public enum inside static class needs to declare the namespace where it is used, despite already imported it with "using"" - a good coding standards would make it illegal to have namespace and class to have same name. And here you have great example why this should be done - so you don't need to use full-qualifying name and for the code clarity.
"Is there any reason for public enum in static class?" - by good standards public enums can be declared in static or instance classes when such enums are used only by these classes and nowhere else.
"Or,I can get rid of the namespace" - you should rename the namespace to something different from your class. Besides, the classes and namespaces should have different naming styles. If your class is named in correct style - AuxValidator
, the namespace should be something like SomeValidation
. Since this is in APP_Code
, you should have no issues renaming it because this is not something like redistribute library where you don't want to upset customers with breaking changes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论