class A有属性b和属性c
当b==1时,c可读可写.
当b!=1时,c为只读的.
求代码.
 

解决方案 »

  1.   

     public int b { get; set; }
            public int c
            {
                get;
                set
                { if (this.b == 1) this.c = value; }
            }
      

  2.   

    bdmh 版主大人说的对,你的需求比较无敌,编译器帮你实现不了了,哈哈,不过还有一种思路就是即使不等于1也可以写但是跑出个异常,不知此方法是否科学,例如:
    private int b = 0;
    private int c = 0;
    public int C
    {
        get{return c}
        set
        {
           if(b != 1)
           {
             //抛出异常
           }
           c = value;
        }
    }
      

  3.   

     class ClassA
        {
            private int b;
            private int c;
            public int B
            {
                set { b = value; }
                get { return b; }
            }
            public int C
            {
                set 
                {
                    if (b == 1)
                    {
                        c = value;
                    }
                }
            
                get { return c; }
            }
        }
      

  4.   

    private int b = 0;
     private int c = 0;
     public int C
     {
         get{return c}
         set
         {
            if(b != 1)
            {
                throw new Exception("b!=1时此属性为只读");
              }
            c = value;
         }
     } 
      

  5.   

    很费劲,先这么写了,看行不行.
    public class A
        {
            public virtual int Count { get; set; }
        }
     public   class A1: A
        {
            public override int Count
            {
                get
                {
                    return base.Count;
                }  
            }
    }
     public class A2: A
        {
            public override int Count
            {
                get
                {
                    return base.Count;
                }
                set
                {
                    base.Count = value;
                }
            }
       
    }