Why do I need a private field that is exposed via public property?

前端 未结 11 716
天涯浪人
天涯浪人 2020-12-11 11:41

I\'m comming from Java world mainly. So, C# properties do look nice.

I know that with C# 3.0 or above I can use Automatic Properties. I like it even more :).

相关标签:
11条回答
  • 2020-12-11 12:03

    In this case you don't require it. If you ever need to do anything else in the setter or getter, though, the automatic property won't work -- you'll need the private field to manipulate.

    0 讨论(0)
  • 2020-12-11 12:04

    Automatic properties in C#, when compiled, end up functionally exactly the same as the above code. The compiler generates a backing field for you.

    So you do need something backing a property - a field or some code - it's just that it's taken care of for you with the shortcut of auto-properties.

    0 讨论(0)
  • 2020-12-11 12:04

    Maybe you want to change the behaviour of the setter or getter at a later point. e.g. log that the value was changed from outside. this would not be possible with a public field without properties

       private int age;
    
       public int Age {
    
         get { return age; }
         set { 
           age = value; 
           Log("value of age changed");
         }     
       }
    

    With a public field age, you had to log this everywhere in the code where you change the value of age.

    0 讨论(0)
  • 2020-12-11 12:04

    Getters and Setters are the public interface that other classes interact with. In complex systems you usually end up doing validation and other work inside the getter and setter.

    The private field is for internal use. If you need to change the value from inside the class but bypass all the extra work you would change the private variable.

    0 讨论(0)
  • 2020-12-11 12:06

    Properties are just a nice way to write a pair of a get and a set method. In Java you would do this:

    private int age;
    
    public int getAge() { return age; }
    public void setAge(int value) { age = value; }
    

    You need the private field to store the age somewhere -- getAge and setAge are just methods.

    The same applies to C# to versions before 3.0:

    private int age;
    
    public int Age
    {
       get { return age; }
       set { age = value; } 
    }
    

    This is a get and a set method, just nicely paired up so you can write x.Age = 21 instead of x.setAge(21).

    With automatic properties, the C# compiler generates the private field for you (but it's still there!):

    public int Age
    {
       get;
       set;
    }
    

    The benefit of automatic properties is that you don't have to create the backing field manually yourself anymore.

    The benefit of the manual version is, that you can add additional logic to the get and set methods, for instance, parameter validation:

    private int age;
    
    public int Age
    {
       get { return age; }
       set { if (value < 0) throw new ArgumentException(); age = value; } 
    }
    
    0 讨论(0)
提交回复
热议问题