为什么我可以手动组合变量以创建Const字符串,而不能使用Concat或字符串插值?

问题描述

在C#中,这是可以接受的:

public const string hello = "Hello";
public const string world = "world";
public const string helloWorld = hello + " " + world;

但是,如果我尝试以下任一方法:

public const string helloWorld = string.Concat(hello," ",world);

public const string helloWorld = $"{hello} {world}";

我收到编译时错误“分配给helloWorld的表达式必须是常量”。

我理解该错误,并且知道在声明public static readonly string变量时可以使用helloWorld,但是在我看来,第二和第三种方法的作用与第一种方法完全相同,因此为什么第一种方法不允许使用它们?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...