C#——索引器的使用案例详解

通过索引器进行照片的添加和查询

 

设计界面:

C#——索引器的使用案例详解

编写代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;  //引入命名空间
namespace 索引器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        Album a = new Album(3);
        //添加照片
        private void button2_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(textBox2 .Text)-1;
            Photo p = new Photo(textBox1 .Text);
            a[i] = p;
            label3.Text = string.Format("照片添加成功!");
            textBox1.Text = "";
            textBox2.Text = "";
        }
        //显示照片
        private void button3_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(textBox2 .Text)-1;
            Photo p = a[i];
            if (p != null)
            {
                label3.Text = string.Format("第{0}张照片的标题是:{1}", i + 1, p.Title);
                textBox1.Text = "";
                textBox2.Text = "";
            }
            else 
            {
                label3.Text = string.Format("没有第{0}张照片",i+1);
                textBox1.Text = "";
                textBox2.Text = "";
            }
        }
        //按标题查找
        private void button1_Click(object sender, EventArgs e)
        {
            Photo p = a[textBox1.Text];
            if (p != null)
            {
                label3.Text = string.Format("找到标题为:{0}的照片", p.Title);
                textBox1.Text = "";
                textBox2.Text = "";
            }
            else 
            {
                label3.Text = string.Format("没有找到标题为:{0}的照片",textBox1 .Text );
                textBox1.Text = "";
                textBox2.Text = "";
            }
        }
    }
    //照片类
    class Photo
    {
        string title;
        public Photo(string title) 
        {
            this.title = title;
        }
        public string Title
        {
            get { return title;}
        }
    }
    //相册类
    class Album
    {
        private Photo[] photos;
        public Album(int c)
        {
            photos = new Photo[c];
        }
        //定义索引器
        public Photo this[int index]  
        {
            get
            { 
                if(index <0||index>=photos .Length)
                {
                    return null;
                }
                return photos[index];
            }
            set 
            {
                if (index < 0 || index >= photos.Length)
                {
                    return;
                }
                photos[index] = value;
            }
        }
        //索引器的重载
        public Photo this[string title]
        {
            get{

                foreach (Photo p in photos)
                { 
                    if(p.Title .IndexOf (title )!=-1)
                    {
                        return p;
                   }                  
                }
                return null;
            }
        }
    }
}
 

运行结果

C#——索引器的使用案例详解