静态字符串的链式插值无法正常工作

问题描述 投票:2回答:2

我使用的是这样的代码:

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#为什么会得到这种行为吗?

c# string string-interpolation
2个回答
7
投票

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

  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;
}

0
投票

您可以像这样切换helloname的位置,

© www.soinside.com 2019 - 2024. All rights reserved.