我正在学习ASP。NET MVC和我可以阅读英文文档,但我真的不明白这段代码中发生了什么:
public class Genre
{
public string Name { get; set; }
}
这意味着什么:{get;设置;} ?
我正在学习ASP。NET MVC和我可以阅读英文文档,但我真的不明白这段代码中发生了什么:
public class Genre
{
public string Name { get; set; }
}
这意味着什么:{get;设置;} ?
这些都是自动属性
这是另一种写带有支持字段属性的方式。
public class Genre
{
private string _name;
public string Name
{
get => _name;
set => _name = value;
}
}
这是一个自动实现的属性。它基本上是在c#中为类创建属性的一种简便方法,而不必为它们定义私有变量。当获取或设置变量值时不需要额外的逻辑时,通常使用它们。
你可以在MSDN的自动实现属性编程指南上阅读更多。
它是一个所谓的auto属性,本质上是以下代码的简写(类似的代码将由编译器生成):
private string name;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
这是做这件事的简短方式:
public class Genre
{
private string _name;
public string Name
{
get => _name;
set => _name = value;
}
}
这是一种将数据成员公开为公共的快捷方式,这样您就不需要显式地创建私有数据成员。c#将为您创建一个私有数据成员。
您可以不使用此快捷方式将数据成员设为公共,但如果您决定更改数据成员的实现以具有某些逻辑,则需要破坏接口。因此,简而言之,它是创建更灵活代码的捷径。
这样的{得到;设置;}语法被称为自动属性,c# 3.0语法
必须使用Visual c# 2008 / csc v3.5或以上版本进行编译。 但是您可以编译低至. net Framework 2.0的输出(不需要运行时或类来支持此特性)。
Get set是属性的访问修饰符。 Get读取属性字段。 Set设置属性值。 Get类似于只读访问。 Set类似于只写访问。 要将属性用作读写,必须同时使用get和set。
所以在我看来{get;设置;}是一个“auto属性”,就像@Klaus和@Brandon说的是写一个带有“支持字段”的属性的速记。在这种情况下:
public class Genre
{
private string name; // This is the backing field
public string Name // This is your property
{
get => name;
set => name = value;
}
}
然而,如果你像我一样——大约一个小时前——你并不真正理解什么是属性和访问器,你也没有最好的理解一些基本的术语。MSDN是学习这些东西的一个很好的工具,但对于初学者来说并不总是容易理解。我将在这里更深入地解释这个问题。
Get和set是访问器,这意味着它们能够访问私有字段(通常从支持字段)中的数据和信息,并且通常从公共属性中这样做(正如您在上面的示例中看到的那样)。
不可否认,上面的说法是相当令人困惑的,所以让我们来看看一些例子。假设这段代码指的是音乐类型。在Genre类中,我们需要不同类型的音乐。假设我们想要拥有3种类型:Hip Hop, Rock和Country。为此,我们将使用类的名称来创建该类的新实例。
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)
现在我们已经创建了Genre类的实例,我们可以使用上面设置的“Name”属性设置类型名称。
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和set属性我们就能同时执行这两个函数。写出g1。名称= "嘻哈";我们特别使用了Name属性中的set函数
Set使用一个隐式变量value。基本上,这意味着任何时候你在set中看到value,它指的是一个变量;“value”变量。当我们写g1时。Name =我们使用=来传递值变量,在本例中为“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"'
}
}
需要注意的是,上面的示例实际上并不是写在代码中。它更像是一个假想的代码,代表了后台正在发生的事情。
现在我们已经设置了类型的g1实例的名称,我相信我们可以通过写入来获得名称
console.WriteLine (g1.Name); //This uses the 'get' function from our 'Name' Property
//and returns the field 'name' which we just set to
//"Hip Hop"
如果我们运行这个,我们会在控制台得到“Hip Hop”。
因此,为了便于解释,我将用输出完成示例
using System;
public class Genre
{
public string Name { get; set; }
}
public class MainClass
{
public static void Main()
{
Genre g1 = new Genre();
Genre g2 = new Genre();
Genre g3 = new Genre();
g1.Name = "Hip Hop";
g2.Name = "Rock";
g3.Name = "Country";
Console.WriteLine ("Genres: {0}, {1}, {2}", g1.Name, g2.Name, g3.Name);
}
}
输出:
"Genres: Hip Hop, Rock, Country"
基本上,它是一个快捷方式:
class Genre{
private string genre;
public string getGenre() {
return this.genre;
}
public void setGenre(string theGenre) {
this.genre = theGenre;
}
}
//In Main method
genre g1 = new Genre();
g1.setGenre("Female");
g1.getGenre(); //Female
在Visual Studio中,如果你在一个类中定义了一个属性X,并且你只想将这个类用作一个类型,在构建项目之后,你会得到一个警告,说“字段X从未被赋值,并且总是有它的默认值”。
通过添加{get;设置;}到X属性,则不会得到此警告。
此外,在Visual Studio 2013及更高版本中,通过添加{get;设置;}你就可以看到对该属性的所有引用。
当属性出现在右侧(RHS)时调用Get。 属性出现在左侧(LHS)时调用 的'='符号
对于自动实现的属性,后台字段在后台工作,不可见。
例子:
public string Log { get; set; }
而对于非自动实现的属性,支持字段是前面的,作为私有作用域变量可见。
例子:
private string log;
public string Log
{
get => log;
set => log = value;
}
另外,这里值得注意的是getter和setter可以使用不同的“支持字段”
The get/set pattern provides a structure that allows logic to be added during the setting ('set') or retrieval ('get') of a property instance of an instantiated class, which can be useful when some instantiation logic is required for the property. A property can have a 'get' accessor only, which is done in order to make that property read-only When implementing a get/set pattern, an intermediate variable is used as a container into which a value can be placed and a value extracted. The intermediate variable is usually prefixed with an underscore. this intermediate variable is private in order to ensure that it can only be accessed via its get/set calls. See the answer from Brandon, as his answer demonstrates the most commonly used syntax conventions for implementing get/set.
它基本上是一种速记。你可以写公共字符串Name {get;设置;}就像在许多例子中一样,但你也可以这样写:
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 throw an error
public int Age{
get { return _age; } // example: Console.WriteLine(Person.Age);
set {
if ( value >= 0) {
_age = value; } // valid example: Person.Age = 25;
}
}
}
它的官方名称是Auto-Implemented Properties,阅读(编程指南)是一个好习惯。 我还推荐教程视频c#属性:为什么使用“get”和“set”。
定义Private变量
在构造函数中加载数据
我已经创建了常量,并从常量加载数据到Selected List类。
public class GridModel
{
private IEnumerable<SelectList> selectList;
private IEnumerable<SelectList> Roles;
public GridModel()
{
selectList = from PageSizes e in Enum.GetValues(typeof(PageSizes))
select( new SelectList()
{
Id = (int)e,
Name = e.ToString()
});
Roles= from Userroles e in Enum.GetValues(typeof(Userroles))
select (new SelectList()
{
Id = (int)e,
Name = e.ToString()
});
}
public IEnumerable<SelectList> Pagesizelist { get { return this.selectList; } set { this.selectList = value; } }
public IEnumerable<SelectList> RoleList { get { return this.Roles; } set { this.Roles = value; } }
public IEnumerable<SelectList> StatusList { get; set; }
}
属性就像一个层,它将私有变量与类的其他成员分开。从外界看来,属性只是一个字段,可以使用.Property访问属性
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return $"{FirstName} {LastName}"; } }
}
FullName是一个属性。有箭头的那个是捷径。从外部世界,我们可以像这样访问FullName:
var person = new Person();
Console.WriteLine(person.FullName);
调用者并不关心您如何实现FullName。但在类内部,你可以随心所欲地更改FullName。
查看微软文档以获得更详细的解释:
https://learn.microsoft.com/en-us/dotnet/csharp/properties
基本上它有助于保护你的数据。考虑这个没有setter和getter的例子,以及有它们的同一个例子。
没有setter和getter
类学生
using System;
using System.Collections.Generic;
using System.Text;
namespace MyFirstProject
{
class Student
{
public string name;
public string gender;
public Student(string cName, string cGender)
{
name = cName;
gender= cGender;
}
}
}
在主
Student s = new Student("Some name", "Superman"); //Gender is superman, It works but it is meaningless
Console.WriteLine(s.Gender);
使用setter和getter
using System;
using System.Collections.Generic;
using System.Text;
namespace MyFirstProject
{
class Student
{
public string name;
private string gender;
public Student(string cName, string cGender)
{
name = cName;
Gender = cGender;
}
public string Gender
{
get { return gender; }
set
{
if (value == "Male" || value == "Female" || value == "Other")
{
gender = value;
}
else
{
throw new ArgumentException("Invalid value supplied");
}
}
}
}
}
主要:
Student s = new Student("somename", "Other"); // Here you can set only those three values otherwise it throws ArgumentException.
Console.WriteLine(s.Gender);
属性是用于封装数据的函数,并允许在每次检索或修改值时执行额外的代码。
c#不同于c++, VB。Net或Objective-C没有一个单独的关键字来声明属性,而是使用两个关键字(get/set)来给出一个简短的语法来声明函数。
But it is quite common to have properties, not because you want to run additional code when data is retrieved or modified, but because either you MIGHT want to do so in the future or there is a contract saying this value has to be a exposed as a property (C# does not allow exposing data as fields via interfaces). Which means that even the abbreviated syntax for the functions is more verbose than needed. Realizing this, the language designers decided to shorten the syntax even further for this typical use case, and added “auto” properties that don’t require anything more than the bare minimum, to wit, the enclosing braces, and either of the two keywords (separated by a semicolon when using both).
在VB。Net中,这些“auto”属性的语法与c#中的长度相同——属性X为字符串vs字符串X {get;Set;},两种情况下都是20个字符。它实现了这样的简洁,因为在正常情况下,它实际上需要3个关键字,而在auto属性的情况下,可以不需要其中的2个。
如果从这两者中删除更多,要么就必须添加一个新的关键字,要么就必须赋予符号或空白以意义。