Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这是我的代码示例:
Button1_Click(object sender, EventArgs e) { PictureBox PB = new PictureBox; PB.Name = TextBox1.Text; }
在此代码中,当用户单击按钮时,PictureBox将创建一个新的类型对象。然后Name将被分配对象。这怎么可能?
PictureBox
Name
我的意思是如果用户再次单击,将创建另一个具有相同引用的对象。这怎么可能?
这怎么可能?
a 上的Name属性Control只是一个字符串属性 - 您可以为其分配任何所需的内容,因此拥有多个具有相同名称的控件与拥有多个显示相同文本的文本框或具有string属性的任何其他类相同。
Control
string
请注意,在您的情况下,您实际上并没有以任何方式使用或存储PictureBox您创建的,因此只要您的方法结束,它将有资格获得 GC。