C#只读属性

using System;
using System.Collections.Generic;
using System.Text;

namespace 面向对象
{
    class Person
    {
        //属性不可更改
        public string FirstName { get;  }
        public string LastName { get;  }
        //属性可更改
        //public string FirstName { get; set; }
        //public string LastName { get; set; }

        public Person(string first, string last)
        {
            FirstName = first;
            LastName = last;
        }

        public override string ToString()
        {
            return FirstName + " " + LastName;
        }

        public string AllCaps()
        {
          //属性不可更改
            return ToString().ToUpper();
            //属性可更改
            //FirstName = FirstName.ToUpper();
            //LastName = LastName.ToUpper();
            //return ToString();
        }
    }

    public class Statr
    {
        public static void Main()
        {
            var p = new Person("Bill", "Wagner");
            Console.WriteLine("The name, in all caps: " + p.AllCaps());
            Console.WriteLine("The name: " + p);
        }
    }
}

C#只读属性

上一篇:WPF数据可视化-趋势图


下一篇:WPF数据可视化-瀑布图