public class Genre
{
private string name; // This is the backing field
public string Name // This is your property
{
get => name;
set => name = value;
}
}
get 和 set 是 accessors ,这意味着他们能够访问 private 字段中的数据和信息(通常来自支持字段),通常是从 publicproperties (如上例所示) .
有's no denying that the above statement is pretty confusing, so let'进入一些例子 . 让's say this code is referring to genres of music. So within the class Genre, we'想要不同类型的音乐 . 假设我们想要3种类型:Hip Hop,Rock和Country . 为此,我们将使用 Class 的名称来创建该类的新 instances .
Genre g1 = new Genre(); //Here we're creating a new instance of the class "Genre"
//called g1. We'll create as many as we need (3)
Genre g2 = new Genre();
Genre g3 = new Genre();
//Note the () following new Genre. I believe that's essential since we're creating a
//new instance of a class (Like I said, I'm a beginner so I can't tell you exactly why
//it's there but I do know it's essential)
现在我们've created the instances of the Genre class we can set the genre names using the '名称' property 已经设置在上面 .
public string Name //Again, this is the 'Name' property
{ get; set; } //And this is the shorthand version the process we're doing right now
我们可以通过编写以下内容将'g1'的名称设置为Hip Hop
g1.Name = "Hip Hop";
这里发生的事情有点复杂 . 就像我之前说过的那样, get 和 set 访问来自您无法访问的私有字段的信息 . get 只能来自该私有字段的 read 信息并将其返回 . set 只能在该私有字段中使用 write 信息 . 但是通过拥有同时拥有 get 和 set 的属性,我们可以完成这两个功能 . 通过编写 g1.Name = "Hip Hop"; ,我们专门使用Name属性中的 set 函数
set 使用名为 value 的隐式变量 . 基本上这意味着当你在 set 中看到"value"时,它指的是一个变量; "value"变量 . 当我们写 g1.Name = 时,我们使用 = 传递 value 变量,在这种情况下是 "Hip Hop" . 所以你基本上可以这样想:
public class g1 //We've created an instance of the Genre Class called "g1"
{
private string name;
public string Name
{
get => name;
set => name = "Hip Hop"; //instead of 'value', "Hip Hop" is written because
//'value' in 'g1' was set to "Hip Hop" by previously
//writing 'g1.Name = "Hip Hop"'
}
}
private string log;
public string Log
{
get => log;
set => log = value;
}
此外,值得注意的是'getter'和'setter'可以使用不同的'后备字段'
25
它基本上是速记 . 您可以在许多示例中编写 public string Name { get; set; } ,但您也可以编写它:
private string _name;
public string Name
{
get { return _name;}
set { _name = value ;} // value is a special keyword here
}
为什么用它?它可用于过滤对属性的访问,例如,您不希望名称包含数字 .
让我给你举个例子:
private class Person {
private int _age; // Person._age = 25; will through an error
public int Age{
get { return _age;} // example: Console.WriteLine(Person.Age);
set {
if ( age >= 0) {
_age = value; } // valid example: Person.Age = 25;
}
}
}
16 回答
这意味着如果您创建Genre类型的变量,您将能够将该变量作为属性进行访问
它是一个所谓的自动属性,基本上是以下的简写(类似的代码将由编译器生成):
据我了解
{ get; set; }
是一个"auto property",就像@Klaus和@Brandon所说的是用"backing field."写一个属性的简写所以在这种情况下:但是,如果你真的明白 properties 和 accessors 是什么,那么对于初学者来说,你并不总是很容易理解 . 所以我将在这里更深入地解释这一点 .
get
和set
是 accessors ,这意味着他们能够访问 private 字段中的数据和信息(通常来自支持字段),通常是从 public properties (如上例所示) .有's no denying that the above statement is pretty confusing, so let'进入一些例子 . 让's say this code is referring to genres of music. So within the class Genre, we'想要不同类型的音乐 . 假设我们想要3种类型:Hip Hop,Rock和Country . 为此,我们将使用 Class 的名称来创建该类的新 instances .
现在我们've created the instances of the Genre class we can set the genre names using the '名称' property 已经设置在上面 .
我们可以通过编写以下内容将'g1'的名称设置为Hip Hop
这里发生的事情有点复杂 . 就像我之前说过的那样,
get
和set
访问来自您无法访问的私有字段的信息 .get
只能来自该私有字段的 read 信息并将其返回 .set
只能在该私有字段中使用 write 信息 . 但是通过拥有同时拥有get
和set
的属性,我们可以完成这两个功能 . 通过编写g1.Name = "Hip Hop";
,我们专门使用Name属性中的set
函数set
使用名为value
的隐式变量 . 基本上这意味着当你在set
中看到"value"时,它指的是一个变量; "value"变量 . 当我们写g1.Name =
时,我们使用=
传递value
变量,在这种情况下是"Hip Hop"
. 所以你基本上可以这样想:重要的是要注意上面的例子实际上没有写在代码中 . 它更像是一个假设代码,代表了后台正在发生的事情 .
所以现在我们已经 set 类型的g1实例的名称,我相信我们可以通过写作来命名
如果我们运行这个,我们将在控制台中获得
"Hip Hop"
.因此,为了解释这个问题,我将完成输出示例
Output:
那些是automatic properties
基本上是另一种用支持字段编写属性的方法 .
这是执行此操作的简短方法:
这是将数据成员公开为公共的快捷方式,因此您无需显式创建私有数据成员 . C#将为您创建一个私有数据成员 .
您可以在不使用此快捷方式的情况下公开您的数据成员,但如果您决定更改数据成员的实现以获得某些逻辑,那么您将需要破坏该接口 . 简而言之,它是创建更灵活代码的捷径 .
基本上,它是一个捷径:
它是C#的auto-implemented property .
它们是公共 property 名称的访问者 .
您可以使用它们来获取/设置类型实例中该属性的值 .
这是一个自动实施的属性 . 它基本上是在C#中为类创建属性的简便方法,而不必为它们定义私有变量 . 它们通常在获取或设置变量值时不需要额外逻辑时使用 .
您可以在MSDN的Auto-Implemented Properties Programming Guide上阅读更多内容 .
get / set模式提供了一个结构,允许在实例化类的属性实例的设置('set')或检索('get')期间添加逻辑,这在需要某些实例化逻辑时非常有用 . property .
属性只能有一个'get'访问器,这样做是为了使该属性为只读
在实现get / set模式时,中间变量用作容器,可以在其中放置值并提取值 . 中间变量通常以下划线为前缀 . 此中间变量是私有的,以确保它只能通过其get / set调用来访问 . 请参阅Brandon的答案,因为他的答案演示了实现get / set的最常用语法约定 .
这种
{ get; set; }
语法称为自动属性,C#3.0语法您必须使用Visual C#2008 / csc v3.5或更高版本进行编译 . 但是您可以编译与.NET Framework 2.0一样低的目标输出(没有运行时或支持此功能所需的类) .
在Visual Studio中,如果在类中定义属性
X
并且只想将此类用作类型,则在构建项目后,将收到一条警告,其中显示为"Field X is never assigned to, and will always has its default value" .通过添加
{ get; set; }
到X
属性,您将不会收到此警告 .此外,在Visual Studio 2013和更高版本中,通过添加
{ get; set; }
,您可以看到对该属性的所有引用 .获取set是属性的访问修饰符 . 获取读取属性字段 . Set设置属性值 . Get就像只读访问权限 . Set就像只写访问 . 要将该属性用作读写,必须使用get和set .
当属性出现在右侧时调用Get(RHS)当属性出现在'='符号的左侧(LHS)时调用Set
对于自动实现的属性,后备字段在场景后面工作并且不可见 .
例:
而对于非自动实现的属性,支持字段是预先存在的,作为私有范围变量可见 .
例:
此外,值得注意的是'getter'和'setter'可以使用不同的'后备字段'
它基本上是速记 . 您可以在许多示例中编写 public string Name { get; set; } ,但您也可以编写它:
为什么用它?它可用于过滤对属性的访问,例如,您不希望名称包含数字 .
让我给你举个例子:
正式称其自动实现属性及其阅读(programming guide)的良好习惯 . 我还推荐教程视频C# Properties: Why use "get" and "set" .