"Read-only" public 属性没有 setters/getters

"Read-only" public properties without setters/getters

C# 是否具有这样的功能(如 Python 的 getter-only 模式)?

class A 
{
   public [read-only] Int32 A_;

   public A() 
   {
      this.A_ = new Int32();
   }

   public A method1(Int32 param1) 
   {
      this.A_ = param1;
      return this;
   }
}

class B 
{
   public B() 
   {
      A inst = new A().method1(123);
      Int32 number = A.A_; // okay
      A.A_ = 456;          // should throw a compiler exception
   }
}

为了获得这个,我可以在 A_ 属性上使用 private 修饰符,并且只实现一个 getter 方法。这样做,为了访问该属性,我应该始终调用 getter 方法...它可以避免吗?

是的。您可以将只读 属性 与私有 setter 一起使用。

Using Properties - msdn

    public string Name    
    {
        get;
        private set;
    }

是的,这是可能的,语法是这样的:

public int AProperty { get; private set; }