欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#属性1(Property)

程序员文章站 2022-07-01 17:02:50
创建一个只读属性 ......

创建一个只读属性

using system;
using system.collections;
using system.collections.generic;
using system.globalization;
using system.linq;
using system.text;
using 编码练习;

namespace 编码练习
{
    //创建类people,里面有两个属性
    public class employee
    {
        public static int numberofemployees;
        private static int counter;
        private string name;

        // a read-write instance property:
        public string name
        {
            get { return name; }
            set { name = value; }
        }

        // a read-only static property:
        public static int counter
        {
            get { return counter; }
        }

        // a constructor:
        public employee()
        {
            // calculate the employee's number:
            counter = ++numberofemployees;
        }
    }
}
public class serchpeople
{
    public static void main()
    {
        employee.numberofemployees = 107;
        employee e1 = new employee();
        e1.name = "cave";
        console.writeline(employee.counter);
    }
}
}