C#属性和readonly类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class AA
    {
        string s;
        public AA()
        {
            s = "123";
        }
        public string S
        {
            get {return s;}
            set { s = value; }
        }
    }
    class Program
    {
        static void change(AA t)
        {
            t.S = "000";
        }
        static void Main(string[] args)
        {
            AA t = new AA();
            Console.WriteLine(t.S);
            change(t);
            Console.WriteLine(t.S);
        }
    }
}

利用S使用户感觉就像直接访问成员变量一样,但实质上是用方法对s的操作

readonly和const类似但能在静态构造函数中赋值。

原文地址:https://www.cnblogs.com/wos1239/p/4382438.html