Shorthand Accessors and Mutators

后端 未结 2 1399
庸人自扰
庸人自扰 2020-12-30 02:56

I am learning C#, and am learning about making fields private to the class, and using Getters and Setters to expose Methods instead of field values.

Are the ge

2条回答
  •  心在旅途
    2020-12-30 03:17

    Yes, the Method2 is the way to go when you have a custom getter and setter function. By default when you use Method1, there will be a default private property handled internally. Please refer this URL for more details.

    Sample:

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

提交回复
热议问题