我正在学习ASP。NET MVC和我可以阅读英文文档,但我真的不明白这段代码中发生了什么:
public class Genre
{
public string Name { get; set; }
}
这意味着什么:{get;设置;} ?
我正在学习ASP。NET MVC和我可以阅读英文文档,但我真的不明白这段代码中发生了什么:
public class Genre
{
public string Name { get; set; }
}
这意味着什么:{get;设置;} ?
当前回答
它基本上是一种速记。你可以写公共字符串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”。
其他回答
它基本上是一种速记。你可以写公共字符串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”。
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.
这是做这件事的简短方式:
public class Genre
{
private string _name;
public string Name
{
get => _name;
set => _name = value;
}
}
这意味着,如果您创建类型为Genre的变量,您将能够将该变量作为属性访问
Genre oG = new Genre();
oG.Name = "Test";
基本上它有助于保护你的数据。考虑这个没有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);