以C#获取/设置更简单的方法?

TAM*_*TAM 4 .net c# properties

嘿所以我目前正在为大学编写一个程序,在创建我的课程时,我正在使用get set我的问题是,是否有一种比设置方法所示的方法更简单的方法.

显示我当前方式的代码段

    private string customer_first_name;
    private string customer_surname;
    private string customer_address;
    public DateTime arrival_time;

//Using accessors to get the private variables
//Using accessors to set the private variables

    public string Customer_First_Name
    {
        get { return customer_first_name; }
        set { customer_first_name = value; }
    }
Run Code Online (Sandbox Code Playgroud)

Ser*_*kiy 9

改为使用自动实现的属性:

public string Customer_First_Name { get; set; }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,编译器将生成用于存储属性值的字段.

顺便说一句,如果你prop在Visual Studio中使用代码片段,你可以节省更多时间.刚开始打字prop并点击Tab- 它会粘贴自动生成属性的片段.所有你需要的 - 提供属性名称和类型.