gpt4 book ai didi

c# - 双引号 "\""?

转载 作者:行者123 更新时间:2023-12-02 05:36:39 31 4
gpt4 key购买 nike

我正在尝试在引号内使用引号。它在文本框内工作,但在我需要字符串的地方不工作。例如,第一行代码有效,但我需要将字符串作为变量。

pictureBox1.Image = MediaLib.Get["chestarmor_105"];

但接下来的 3 个没有。我最终尝试将 richTextBox1.Text 用作变量,因为它在那里看起来很好,但运气不好。

string chestArmor = "chestarmor_105";
richTextBox2.Text = "\"" + chestArmor + "\"";
pictureBox1.Image = MediaLib.Get[richTextBox2.Text];

我已经尝试了许多不同的 ""变体。我错过了什么?谢谢。

最佳答案

第一行中的双引号是编译器用来区分字符串文字和变量名的语法产物。它们不在字符串中;字符串是 chestarmor_105,没有双引号。

如果你写

pictureBox1.Image = MediaLib.Get[chestarmor_105]; // no quotes

编译器会认为 chestarmor_105 代表一个标识符;您将 chestarmor_105 用双引号括起来,告诉编译器您希望按字面意义将其用作 14 个字符的字符串,而不是变量名。编译器然后删除双引号,并将该值用作字符串。

这应该有效:

string chestArmor = "chestarmor_105";
richTextBox2.Text = chestArmor;
pictureBox1.Image = MediaLib.Get[richTextBox2.Text];

关于c# - 双引号 "\""?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11597417/

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