Naming convention for private fields

后端 未结 4 580
萌比男神i
萌比男神i 2020-12-16 10:16

First, I know this question has been asked several times before and that in the end, it is mostly a matter of personal preference, but reading all the threads about the subj

4条回答
  •  旧巷少年郎
    2020-12-16 11:07

    Using this.age could help distinguish between the backing store of your Age property and an age parameter for a method on your object:

    public bool CheckIfOlderThan(int age)
    {
       // in here, just using "age" isn't clear - is it the method parameter? 
       // The internal field?? Using this.age make that clear!
       return (this.age >= age); 
    }
    

    Of course, in this case, you could also give your parameter a less confusing name to avoid any clashes....

    But in the actual definition of the property - reading and storing its value in the backing store - adding the this. doesn't really add anything. Some people I know just prefer to use the this. prefix all the time - not just when it's needed - personal preference, really...

提交回复
热议问题