get 和 set :怎样做才是只读 get ?怎样做才是只写 set ?

来源:互联网 发布:户外跑步知乎 编辑:程序博客网 时间:2024/04/30 21:42

代码如下:

namespace C_sharp学习{    ///<summary>    ///学生类    ///</summary>    public class Student    {        private int nAge;        public int Age        {            get            {                return this.nAge;            }            set            {                if (value != this.nAge)                    this.nAge = value;            }        }        ///<summary>        ///主函数        ///</sunmary>        static void Main(string[] args)        {                Student s=new Student();                s.Age=20;                Console.WriteLine(s.Age);                Console.ReadLine();        }    }}


0 0