Is there a way to make readonly (not just private) automatic properties?

前端 未结 6 600
日久生厌
日久生厌 2020-12-31 13:02

Automatic properties let me replace this code:

private MyType myProperty;
public MyType MyProperty
{
    get { return myPropertyField; }
}

6条回答
  •  长情又很酷
    2020-12-31 13:56

    readonly can only be applied to fields, so I believe not.

    Could you not just use:

    public readonly MyType MyProperty;
    

    as it is then only assignable from the constructor anyway?

提交回复
热议问题