When to use get; set; in c#

前端 未结 9 1218
庸人自扰
庸人自扰 2020-12-29 20:59

I\'m failing to understand what the difference is between initializing a variable, getting its value like this:

 //define a local variable.
   int i;

   i=          


        
9条回答
  •  我在风中等你
    2020-12-29 21:31

    It is basically concept of Object Oriented Programming.

    when you use int i; ( This consider as field and this field can be internal usage as well as it can be use from outside current class depending upon access modifier. (Public , Private , Protected)

    Now when you use get; set; it is property of class. It can also set from other class but diffrence it is access like method and it provide other functionality like notification of property change and all that.

    Use filed when you don't want any control over it but if you want to control then use property. Also you can have get and set with public , private and protected which can also provide more flexibility interm of encaptulation.

提交回复
热议问题