首页 > 其他 > 详细

C# 索引器

时间:2014-05-10 05:43:32      阅读:344      评论:0      收藏:0      [点我收藏+]

使用索引器的目的是为了能够像数组一样访问类中的数组型的对象。

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuoYinQiTest
{
    
    class Persons
    {
        private int currentIndex = 0;
        private String[] names = new String[10];
        public String this[int index]{
            get
            {
                return names[index];
            }
            set
            {
                names[index] = value;
                currentIndex++;
            }
        }
        public void printAll()
        {
            for (int i = 0; i < currentIndex; i++)
            {
                Console.WriteLine(names[i]);
            }
            Console.WriteLine("输出完毕");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Persons persons = new Persons();
            persons[0] = "zhangsan";
            persons[1] = "lisi";
            persons.printAll();
            Console.Read();
        }
    }
}
bubuko.com,布布扣

 

C# 索引器,布布扣,bubuko.com

C# 索引器

原文:http://www.cnblogs.com/wuyou/p/3719525.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!