gpt4 book ai didi

c# - 静态字符串的链式插值无法按预期工作

转载 作者:行者123 更新时间:2023-12-02 06:46:35 27 4
gpt4 key购买 nike

我使用的是这样的代码:

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# 会出现这种行为吗?

最佳答案

静态字段按照声明的顺序进行初始化。那么会发生什么:

  1. 最初,helloname 都是nullnum 是一个常量。
  2. hello 已初始化。 name 仍然是 null。但是,num 是一个 const,因此可以正确替换。 hello 的值为 "hola 4"
  3. 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;
}

关于c# - 静态字符串的链式插值无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59784765/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com