gpt4 book ai didi

c# - C# 6 字符串插值的默认区域性是什么?

转载 作者:IT王子 更新时间:2023-10-29 04:06:17 25 4
gpt4 key购买 nike

在 C# 6 中,新字符串插值的默认区域性是什么?

我见过关于不变文化和当前文化的相互矛盾的报道。

我想要一个明确的答案,我会为不变量祈祷。

最佳答案

在 C# 中使用字符串插值被编译为对 String.Format 的简单调用。用TryRolsyn可以看到这:

public void M()
{
string name = "bar";
string result = $"{name}";
}

编译成这样:

public void M()
{
string arg = "bar";
string text = string.Format("{0}", arg);
}

很明显,这没有使用接受格式提供程序的重载,hence it uses the current culture .

但是,您可以将插值编译为 FormattbleString,这样可以将格式和参数分开,并在生成最终字符串时传递特定的文化:

FormattableString formattableString = $"{name}";
string result = formattableString.ToString(CultureInfo.InvariantCulture);

既然(如您所愿)使用 InvariantCulture 非常普遍,特别是有一个简写形式:

string result = FormattableString.Invariant($"{name}");

关于c# - C# 6 字符串插值的默认区域性是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33203261/

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