2023-06-02 09:00:01

{get;设置;}语法在c# ?

我正在学习ASP。NET MVC和我可以阅读英文文档,但我真的不明白这段代码中发生了什么:

public class Genre
{
    public string Name { get; set; }
}

这意味着什么:{get;设置;} ?


当前回答

基本上,它是一个快捷方式:

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

其他回答

基本上它有助于保护你的数据。考虑这个没有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);

这些都是自动属性

这是另一种写带有支持字段属性的方式。

public class Genre
{
    private string _name;

    public string Name 
    { 
      get => _name;
      set => _name = value;
    }
}

基本上,它是一个快捷方式:

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

属性是用于封装数据的函数,并允许在每次检索或修改值时执行额外的代码。

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个。

如果从这两者中删除更多,要么就必须添加一个新的关键字,要么就必须赋予符号或空白以意义。

这样的{得到;设置;}语法被称为自动属性,c# 3.0语法

必须使用Visual c# 2008 / csc v3.5或以上版本进行编译。 但是您可以编译低至. net Framework 2.0的输出(不需要运行时或类来支持此特性)。