我正在学习ASP。NET MVC和我可以阅读英文文档,但我真的不明白这段代码中发生了什么:
public class Genre
{
public string Name { get; set; }
}
这意味着什么:{get;设置;} ?
我正在学习ASP。NET MVC和我可以阅读英文文档,但我真的不明白这段代码中发生了什么:
public class Genre
{
public string Name { get; set; }
}
这意味着什么:{get;设置;} ?
当前回答
当属性出现在右侧(RHS)时调用Get。 属性出现在左侧(LHS)时调用 的'='符号
对于自动实现的属性,后台字段在后台工作,不可见。
例子:
public string Log { get; set; }
而对于非自动实现的属性,支持字段是前面的,作为私有作用域变量可见。
例子:
private string log;
public string Log
{
get => log;
set => log = value;
}
另外,这里值得注意的是getter和setter可以使用不同的“支持字段”
其他回答
定义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; }
}
属性是用于封装数据的函数,并允许在每次检索或修改值时执行额外的代码。
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个。
如果从这两者中删除更多,要么就必须添加一个新的关键字,要么就必须赋予符号或空白以意义。
这是做这件事的简短方式:
public class Genre
{
private string _name;
public string Name
{
get => _name;
set => _name = value;
}
}
它基本上是一种速记。你可以写公共字符串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”。
在Visual Studio中,如果你在一个类中定义了一个属性X,并且你只想将这个类用作一个类型,在构建项目之后,你会得到一个警告,说“字段X从未被赋值,并且总是有它的默认值”。
通过添加{get;设置;}到X属性,则不会得到此警告。
此外,在Visual Studio 2013及更高版本中,通过添加{get;设置;}你就可以看到对该属性的所有引用。