双引号 "\""?

top*_*eel 2 c# string double-quotes

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

pictureBox1.Image = MediaLib.Get["chestarmor_105"];
Run Code Online (Sandbox Code Playgroud)

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

string chestArmor = "chestarmor_105";
richTextBox2.Text = "\"" + chestArmor + "\"";
pictureBox1.Image = MediaLib.Get[richTextBox2.Text];
Run Code Online (Sandbox Code Playgroud)

我尝试了很多不同的"变体".我错过了什么?谢谢.

das*_*ght 5

第一行中的双引号是编译器的语法工件,用于区分字符串文字和变量名称.它们不在字符串中; 字符串是chestarmor_105,没有双引号.

如果你写

pictureBox1.Image = MediaLib.Get[chestarmor_105]; // no quotes
Run Code Online (Sandbox Code Playgroud)

编译器会认为chestarmor_105代表一个标识符; 你chestarmor_105用双引号括起来告诉编译器你想把它用作14个字符的字符串,而不是变量名.然后编译器删除双引号,并将该值用作字符串.

这应该工作:

string chestArmor = "chestarmor_105";
richTextBox2.Text = chestArmor;
pictureBox1.Image = MediaLib.Get[richTextBox2.Text];
Run Code Online (Sandbox Code Playgroud)