C# 属性(Property)


简单示例,直接上代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;//在C#中使用ArrayList必须引用Collections类


namespace Study
{
    class IndexedNames
    {
        static public int size = 10;
        private String[] namelist = new String[size];
        public IndexedNames()
        {
            for(int i=0;i<size;i++)
            {
                namelist[i]="N.A";
            }
        }
        
        public String this [int index]
        {
            get
            {
                string tmp;
                if((index>=0)&&(index<size))
                {
                    tmp = namelist[index];
                    return tmp;
                }
                else
                {
                    return "";
                }
            }
            set
            {
                namelist[index] = value;
            }
        }
    }
  
    class program
    {
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Green;

            IndexedNames T_Names = new IndexedNames();
            T_Names[0] = "DaXiang";
            T_Names[1] = "ShiZi";
            T_Names[2] = "LaoHu";

            for (int ix = 0; ix < IndexedNames.size; ix++)
            {
                Console.WriteLine("T_Names{0}={1}", ix, T_Names[ix]);
            }
            Console.ReadLine();

        }
    }
}

运行结果:

其他大侠的,示例代码:

 namespace Study
    {
        class Program
        {
            static void Main(string[] args)
            {
               Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.DarkYellow;

                ScoreIndex s = new ScoreIndex();
                s["张三", 1] = 90;
                s["张三", 2] = 100;
                s["张三", 3] = 80;
                s["李四", 1] = 60;
                s["李四", 2] = 70;
                s["李四", 3] = 50;
                Console.WriteLine("张三课程编号为1的成绩为:" + s["张三", 1]);
                Console.WriteLine("张三的所有成绩为:");
                ArrayList temp;
                temp = s["张三"];
                foreach (IndexClass b in temp)
                {
                    Console.WriteLine("姓名:" + b.Name + "课程编号:" + b.CourseID + "分数:" + b.Score);
                }
                Console.ReadLine();
            }
        }
    
        class IndexClass
        {
            private string _name;
            private int _courseid;
            private int _score;
            public IndexClass(string _name, int _courseid, int _score)
            {
                this._name = _name;
                this._courseid = _courseid;
                this._score = _score;
            }
            public string Name
            {
                get { return _name; }
                set { this._name = value; }
            }
            public int CourseID
            {
                get { return _courseid; }
                set { this._courseid = value; }
            }
            public int Score
            {
                get { return _score; }
                set { this._score = value; }
            }
        }
    
        class ScoreIndex
        {
            private ArrayList arr;
            public ScoreIndex()
            {
                arr = new ArrayList();
            }
            public int this[string _name, int _courseid]
            {
                get
                {
                    foreach (IndexClass a in arr)
                    {
                        if (a.Name == _name && a.CourseID == _courseid)
                        {
                            return a.Score;
                        }
                    }
                    return -1;
                }
                set
                {
                    arr.Add(new IndexClass(_name, _courseid, value)); //arr["张三",1]=90
                }
            }
    
            //重载索引器
            public ArrayList this[string _name]
            {
                get
                {
                    ArrayList temp = new ArrayList();
                    foreach (IndexClass b in arr)
                    {
                        if (b.Name == _name)
                        {
                            temp.Add(b);
                        }
                    }
                    return temp;
                }
            }
        }
    }

运行结果:

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值