静态字符串的链式插值无法按预期工作
Chained interpolation of static string doesn't work as expected
我使用的是这样的代码:
public static class Program {
public static void Main() {
Console.WriteLine(hello);
}
internal static readonly string hello = $"hola {name} {num}";
internal static readonly string name = $"Juan {num}";
public const int num = 4;
}
在这种情况下,当我得到 hello 的值时它返回给我 "hola 4",所以似乎在插入另一个使用插值的字符串时出现问题。
我的预期行为是 "hola Juan 4 4",或者如果语言不支持这种链式插值,则会在编译时出错。
有人知道为什么 C# 会出现这种行为吗?
静态字段按照声明的顺序进行初始化。所以会发生什么:
- 最初,
hello
和name
都是null
。 num
是一个常量。
hello
已初始化。 name
仍然是 null
。但是,num
是一个常量,因此可以正确替换。 hello
的值为 "hola 4"
name
已初始化。
为什么 num
是一个 const 这个事实会有所不同?请记住,编译器在编译时将 const 的值直接替换到使用它的地方。因此,如果您查看编译器生成的内容,您会看到:
public static class Program
{
internal static readonly string hello = string.Format("hola {0} {1}", name, 4);
internal static readonly string name = string.Format("Juan {0}", 4);
public const int num = 4;
public static void Main()
{
Console.WriteLine(hello);
}
}
(由 SharpLab 提供)
注意 const 的值是如何编译到它使用的地方的。
当你有相互依赖的静态字段时,你要么需要非常小心它们的声明顺序,要么通常更安全(也更易读!)只使用静态构造函数:
public static class Program {
static Program() {
name = $"Juan {num}";
hello = $"hola {name} {num}";
}
public static void Main() {
Console.WriteLine(hello);
}
internal static readonly string hello;
internal static readonly string name;
public const int num = 4;
}
你可以调换hello
和name
的位置,比如
internal static readonly string name = $"Juan {num}";
internal static readonly string hello = $"hola {name} {num}";
Because when hello
is assigning, the name
is not assigned yet.
重新排序name
和hello
后,将按您的需要打印
Prints => "hola Juan 4 4"
我使用的是这样的代码:
public static class Program {
public static void Main() {
Console.WriteLine(hello);
}
internal static readonly string hello = $"hola {name} {num}";
internal static readonly string name = $"Juan {num}";
public const int num = 4;
}
在这种情况下,当我得到 hello 的值时它返回给我 "hola 4",所以似乎在插入另一个使用插值的字符串时出现问题。 我的预期行为是 "hola Juan 4 4",或者如果语言不支持这种链式插值,则会在编译时出错。
有人知道为什么 C# 会出现这种行为吗?
静态字段按照声明的顺序进行初始化。所以会发生什么:
- 最初,
hello
和name
都是null
。num
是一个常量。 hello
已初始化。name
仍然是null
。但是,num
是一个常量,因此可以正确替换。hello
的值为"hola 4"
name
已初始化。
为什么 num
是一个 const 这个事实会有所不同?请记住,编译器在编译时将 const 的值直接替换到使用它的地方。因此,如果您查看编译器生成的内容,您会看到:
public static class Program
{
internal static readonly string hello = string.Format("hola {0} {1}", name, 4);
internal static readonly string name = string.Format("Juan {0}", 4);
public const int num = 4;
public static void Main()
{
Console.WriteLine(hello);
}
}
(由 SharpLab 提供)
注意 const 的值是如何编译到它使用的地方的。
当你有相互依赖的静态字段时,你要么需要非常小心它们的声明顺序,要么通常更安全(也更易读!)只使用静态构造函数:
public static class Program {
static Program() {
name = $"Juan {num}";
hello = $"hola {name} {num}";
}
public static void Main() {
Console.WriteLine(hello);
}
internal static readonly string hello;
internal static readonly string name;
public const int num = 4;
}
你可以调换hello
和name
的位置,比如
internal static readonly string name = $"Juan {num}";
internal static readonly string hello = $"hola {name} {num}";
Because when
hello
is assigning, thename
is not assigned yet.
重新排序name
和hello
后,将按您的需要打印
Prints => "hola Juan 4 4"