gpt4 book ai didi

c# - 对常量使用嵌套类?

转载 作者:可可西里 更新时间:2023-11-01 03:00:06 24 4
gpt4 key购买 nike

使用嵌套类对常量进行分组有什么问题?

像这样:

public static class Constants
{
public static class CategoryA
{
public const string ValueX = "CatA_X";
public const string ValueY = "CatA_Y";
}
public static class CategoryB
{
public const string ValueX = "CatB_X";
public const string ValueY = "CatB_Y";
}
}

这样使用:

Console.WriteLine(Constants.CategoryA.ValueY);
Console.WriteLine(Constants.CategoryB.ValueX);

您还可以将“常量”类设为部分...

最佳答案

有一些 guideline (针对 fx 4.5 更新)针对公共(public)嵌套类:

√ DO use nested types when the relationship between the nested type and its outer type is such that member-accessibility semantics are desirable.

X AVOID publicly exposed nested types. The only exception to this is if variables of the nested type need to be declared only in rare scenarios such as subclassing or other advanced customization scenarios.

X DO NOT use nested types if the type is likely to be referenced outside of the containing type.

我认为你的例子符合第一点(即:你很好)。

关于c# - 对常量使用嵌套类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2945538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com