一位同事刚刚在 C# 中创建了以下构造(示例代码已简化)。他的目标是缩短其余代码中所有预定义字符串的符号。
public struct PredefinedStrings
{
public const string VeryLongName = "Very Long Name";
public const string AnotherVeryLongName = "Another Very Long Name";
public const string TheLastVeryLongName = "The Last Very Long Name";
}
public static void MethodThatUsesTheNames()
{
Console.WriteLine(PredefinedStrings.VeryLongName);
Console.WriteLine(PredefinedStrings.AnotherVeryLongName);
Console.WriteLine(PredefinedStrings.TheLastVeryLongName);
}
尽管这对他来说似乎工作得很好,但我不禁想知道他是否应该使用静态类而不是结构,或者是否有更优雅的方式来实现这一点。
这样做的首选方法是什么?还请解释原因。