properties in C#

前端 未结 4 1342
慢半拍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:08

    Actually these aren't really different, in both cases you have a private field that corresponds to your property, but in the first case it is generated by the compiler and hidden.

    If you need to use the variable behind the property quite often in your class, I think it's better to declare your property the old way (2nd one), because each time you will access it this will call the getter if you do it the "new" way.

    If you only need it to be used from outside your class (or in most of cases), then you can go with the "new" way (1st one)

提交回复
热议问题