C# protected property or field

后端 未结 5 1396
离开以前
离开以前 2021-01-01 13:47

Do you think it\'s better to always make protected class members an auto-implemented protected property to keep isolation or make it protected field is enough?



        
5条回答
  •  感动是毒
    2021-01-01 14:08

    You should never allow direct access to a member variable from outside your class. Either use an auto-generated property or a property with a backing field. If you allow direct access, it can lead to some really bad debugging headaches when multiple methods change that value in a derivation chain and you don't know which one is causing the bug.

提交回复
热议问题