【发布时间】:2013-09-08 14:58:53
【问题描述】:
我有一组相互关联的常量字符串:
private const string tabLevel1 = "\t";
private const string tabLevel2 = "\t\t";
private const string tabLevel3 = "\t\t\t";
...
我正在寻找一种更优雅的方式来声明这些,例如:
private const string tabLevel1 = "\t";
private const string tabLevel2 = REPEAT_STRING(tabLevel1, 2);
private const string tabLevel3 = REPEAT_STRING(tabLevel1, 3);
...
是否有一些预处理指令或其他方式来实现这一点?
附:
我已经知道const string tabLevel2 = tabLevel1 + tabLevel1; 有效,可能是由于this。我正在寻找任意n 的一般情况。
编辑
我想澄清为什么我需要const 而不是static readonly:常量用作属性装饰器的参数,例如[GridCategory(tabLevel2)],并且必须在编译时知道。
【问题讨论】:
-
您希望它重复多少次?我认为关心并不重要。 常量是你应该感到满意的东西,即使你必须输入数百行来声明它们。
Win32 Constants就是一个例子。
标签: c# string c-preprocessor constants