C# C中的砂矿夹持器#

C# C中的砂矿夹持器#,c#,.net,windows,C#,.net,Windows,如何在C#windows窗体应用程序中使用占位符作为字符串连接 我尝试过控制台应用程序,它工作正常,但在windows窗体应用程序上它不工作 private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello {0}", textBox1.Text); } 输出应该类似于“Hello+TextBox.Text”,但它给出了“Hello{0}”使用String.Format() 做一个 在dotn

如何在C#windows窗体应用程序中使用占位符作为字符串连接

我尝试过控制台应用程序,它工作正常,但在windows窗体应用程序上它不工作

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Hello {0}", textBox1.Text);
}
输出应该类似于“Hello+TextBox.Text”,但它给出了“Hello{0}”

使用
String.Format()

做一个

在dotnetcore和旧框架之间,事情的处理方式有所不同。

的第二个参数是为
消息框的标题保留。所以这个代码
MessageBox.Show(“Hello{0}”,textBox1.Text)
显示一个
MessageBox
,其中
Hello{0}
作为文本,
textBox1.text
作为
MessageBox
的标题。您可以像这样使用
String.Format

string result = string.Format("Hello {0}", textBox1.Text);
MessageBox.Show(result);
C#6.0支持字符串插值

MessageBox.Show($"Hello {textbox1.Text}")

或者您可以像其他人所说的那样使用string.Format。

您正在寻找
string.Format
这与.Net核心无关。
string result = string.Format("Hello {0}", textBox1.Text);
MessageBox.Show(result);
MessageBox.Show($"Hello {textbox1.Text}")