C# 这是什么意思 ?公共名称{get; 放;}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1310223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does this mean ? public Name {get; set;}
提问by Slabo
I see this quiet often in C# documentation. But what does it do?
我经常在 C# 文档中看到这种安静。但是它有什么作用呢?
public class Car
{
public Name { get; set; }
}
采纳答案by Bryan Watts
It is shorthand for:
它是以下的简写:
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
The compiler generates the member variable. This is called an automatic property.
编译器生成成员变量。这称为自动属性。
回答by Mehrdad Afshari
It's an automatic read-write property. It's a C# 3.0 addition. Something like:
这是一个自动读写属性。这是 C# 3.0 的补充。就像是:
public class Car {
private string name;
public string Name { get { return name; } set { name = value; } }
}
except that you can't directly access the backing field.
除了您不能直接访问支持字段。
回答by Darthg8r
It is the equivilent of doing:
这相当于做:
private string _Text;
public string Text
{
get { return _Text; }
set { _Text = value; }
}
Except you don't have access to the private variable while inside the class.
除非您在课堂内无权访问私有变量。
回答by Jeff Donnici
It's called an Auto-Implemented Property and is new to C# 3.0. It's a cleaner syntax when your access to the property doesn't need any special behavior or validation. It's similar in function to:
它被称为自动实现的属性,是C# 3.0 的新特性。当您对属性的访问不需要任何特殊行为或验证时,这是一种更清晰的语法。它的功能类似于:
public class Car
{
private string _name;
public string Name
{
get { return _name; }
set {_name = value; }
}
}
So it saves a fair amount of code, but leaves you the option later to modify the accessor logic if behavior or rules need to change.
因此,它节省了大量代码,但如果行为或规则需要更改,您可以选择稍后修改访问器逻辑。
回答by adatapost
SUMMARY:In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.
总结:在 C# 3.0 及更高版本中,当属性访问器中不需要额外的逻辑时,自动实现的属性使属性声明更加简洁。
回答by Mohammed Moinuddin
In simple terms they are referred as property accessors. Their implementation can be explained as below
简单来说,它们被称为属性访问器。它们的实现可以解释如下
1.get{ return name} The code block in the get accessor is executed when the property is Read.
1.get{return name} 属性为Read时执行 get accessor 中的代码块。
2.set{name = value} The code block in the set accessor is executed when the property is Assigneda new value.
2.set{name = value} set accessor 中的代码块在属性被赋值新值时执行。
Eg.(Assuming you are using C#)
例如(假设您使用的是 C#)
class Person
{
private string name; // the name field
public string Name // the Name property
{
get
{
return name;
}
set
{
name = value;
}
}
}
Now when you refer to this property as below
Person p = new Person();// Instantiating the class or creating object 'p' of class 'Person'
System.Console.Write(p.Name); //The get accessor is invoked here
现在,当您如下引用此属性时
Person p = new Person();// 实例化类或创建类 'Person' 的对象 'p'
System.Console.Write(p.Name); //The get accessor is invoked here
The get accessor is invoked to Readthe value of property i.e the compiler tries to read the value of string 'name'.
get访问被调用,以读取的财产即编译器试图值读取字符串“名”的值。
2.When you Assigna value(using an argument) to the 'Name' property as below
2.当您将值(使用参数)分配给“名称”属性时,如下所示
Person p = new Person();
p.Name = "Stack" // the set accessor is invoked here
Console.Writeline(p.Name) //invokes the get accessor
Console.ReadKey(); //Holds the output until a key is pressed
The set accessor Assignsthe value 'Stack" to the 'Name property i.e 'Stack' is stored in the string 'name'.
set 访问器将值“Stack”分配给“Name”属性,即“Stack”存储在字符串“name”中。
Ouput:
输出:
Stack
堆