Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 引用另一个常量字符串的常量字符串-如何避免在没有readonly的情况下干燥 公共静态类Abc { public const字符串占位符=“{$content}”; 公共常量字符串模式=$“{Placeholder}”; }_C# - Fatal编程技术网

C# 引用另一个常量字符串的常量字符串-如何避免在没有readonly的情况下干燥 公共静态类Abc { public const字符串占位符=“{$content}”; 公共常量字符串模式=$“{Placeholder}”; }

C# 引用另一个常量字符串的常量字符串-如何避免在没有readonly的情况下干燥 公共静态类Abc { public const字符串占位符=“{$content}”; 公共常量字符串模式=$“{Placeholder}”; },c#,C#,如何正确解决这一问题以避免干裂(不要重复)?我知道我可以使用静态只读,但这是一个有点不同的事情(即使它工作….)。我认为应该有更好的办法?还是真的没有?虽然不能调用方法初始化常量值,但允许使用运算符。如果需要创建包含另一个常量值的常量,请使用加号运算符连接字符串值 public static class Abc { public const string Placeholder = "{$content}"; public const string Pattern = $"<

如何正确解决这一问题以避免干裂(不要重复)?我知道我可以使用静态只读,但这是一个有点不同的事情(即使它工作….)。我认为应该有更好的办法?还是真的没有?

虽然不能调用方法初始化常量值,但允许使用运算符。如果需要创建包含另一个常量值的常量,请使用加号运算符连接字符串值

public static class Abc
{
    public const string Placeholder = "{$content}";
    public const string Pattern = $"<div class=\"embed-responsive\">{Placeholder}</div>";
}
公共静态类Abc
{
public const字符串占位符=“{$content}”;
公共常量字符串模式=“占位符+”;
}

虽然不能调用方法初始化常量值,但允许使用运算符。如果需要创建包含另一个常量值的常量,请使用加号运算符连接字符串值

public static class Abc
{
    public const string Placeholder = "{$content}";
    public const string Pattern = $"<div class=\"embed-responsive\">{Placeholder}</div>";
}
公共静态类Abc
{
public const字符串占位符=“{$content}”;
公共常量字符串模式=“占位符+”;
}

static readonly有什么问题?@Dr.Fre:有关const和readonly之间差异的说明,请参阅static readonly有什么问题?@Dr.Fre:有关const和readonly之间差异的说明,请参阅