Private 'set' in C# - having trouble wrapping my brain around it

前端 未结 9 2397
傲寒
傲寒 2021-02-12 10:34

I\'ve seen a lot of example code written using something like (please forgive how horribly canned this is):

public class Test
{
   public object Thingy { get; pr         


        
9条回答
  •  后悔当初
    2021-02-12 11:00

    This is just laziness that comes about from auto-properties. Before auto properties were around, people would implement the getter and omit the setter for properties which are meant to be read-only.

    public class Test
    {
       private /*readonly*/ Type _thingy;
       public Type Thingy { get { return _thingy; } }
    }
    

    Hopefully, C# 5 will allow you to create auto-properties with a getter only - because that's what everyone wants. (They should make readonly setters in auto-props too, I need that badly)

提交回复
热议问题