我们为什么要做这个:
private string StatusText
{
set { toolStripStatusLabel1.Text = value; }
}
Run Code Online (Sandbox Code Playgroud)
而不仅仅是这个?
private string StatusText
{
toolStripStatusLabel1.Text = value;
}
Run Code Online (Sandbox Code Playgroud)
我不明白使用套装的意义吗?
Hei*_*nzi 16
这是两件完全不同的事情.
这是一种方法:
private string StatusText()
{
toolStripStatusLabel1.Text = value;
}
Run Code Online (Sandbox Code Playgroud)
这被称为:
StatusText();
Run Code Online (Sandbox Code Playgroud)
(并且不会编译,因为value无法找到局部变量).为了使它工作,你需要像这样写:
private string StatusText(string value)
{
toolStripStatusLabel1.Text = value;
}
Run Code Online (Sandbox Code Playgroud)
并称之为:
StatusText("bla");
Run Code Online (Sandbox Code Playgroud)
另一方面,这是属性的定义:
private string StatusText
{
set { toolStripStatusLabel1.Text = value; }
}
Run Code Online (Sandbox Code Playgroud)
其setter(因此关键字set)被调用如下:
StatusText = "bla";
Run Code Online (Sandbox Code Playgroud)
因为也可能有一个得到:
get { return toolStripStatusLabel1.Text; }
Run Code Online (Sandbox Code Playgroud)
属性是语法糖.编译时,您将有两种方法get_[property name]和set_[property name].如果你只有set方法,那么只有set_[propety name]在IL中.