properties in C#

前端 未结 4 1322
慢半拍i
慢半拍i 2020-12-31 05:00

Why are we able to write

public int RetInt
{
   get;set;
}

instead of

public int RetInt
{
   get{return someInt;}set{someI         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-31 05:14

    How are these two different ?

    There are different at least by 2 points:

    1. In normal property you have to define a field before (someInt in your case)
    2. In normal property you can set a breakpoint into the set/get modifiers, instead in auto property can not do that.

    In other words: if you need "just property", use auto-properties, if you need more control over workflow (raise an event on set, debug, run other stuff inside), use "normal" properties.

提交回复
热议问题