码迷,mamicode.com
首页 > 其他好文 > 详细

学习系列之索引器与属性

时间:2017-10-30 21:52:11      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:ati   selected   lis   data   记录   nts   str   com   查看   

实际工作中我们遇到索引器的地方有很多,我们都是似曾相似,今天我们一起来揭开他们的神秘面纱

项目开发中在获得DataGridView控件的某列值时:dgvlist.SelectedRows[0].Cells[0].Value;

在获得ListView控件的某列值时:listView1.SelectedItems[0].SubItems[0].Text;

在读取数据库记录给变量赋值时:result=dr["StudentName"].ToString();

记得Dictionary中根据key值来获取Value值时:dic["key"]等等

一、索引器的定义

C#中的类成员可以是任意类型,包括数组和集合。当一个类包含了数组和集合成员时,索引器将大大简化对数组或集合成员的存取操作。

定义索引器的方式与定义属性有些类似,其一般形式如下:

 [修饰符] 数据类型 this[索引类型 index]
  {
    get{//获得属性的代码}                                              

    set{ //设置属性的代码}
  }

二、索引器的本质是属性

下面我们以一个例子来了解索引器的用法和原理:

1.创建一个Test类,里面定义一个数组和一个索引器

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 索引器
{
   public class Test
    {
       //01.首先定义一个数组
       private string[] name=new string[2];

       //02.根据创建索引器的语法定义一个索引器,给name数组赋值和取值
       public string this[int index]
       {
           get { return name[index]; }
           set { name[index] = value; }
       }
    }
}

 

2.在Main方法中通过索引器给Test类中的数组赋值

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 索引器
{
    class Program
    {
        static void Main(string[] args)
        {
            //01.首先你得实例化Test类的对象
            Test test=new Test();
            //02.利用索引器给Test类中的数组赋值
            test[0] = "张总";
            test[1] = "吴总";
            //03.打印查看效果
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine(test[i]);
            }
            Console.ReadLine();
        }
    }
}

 

3.效果如下:

技术分享

上面说到 索引器的本质是属性。是的,我们可以发现上面定义索引器的代码的IL(中间语言)是(属性的格式):

技术分享

四、实际开发例子:

设计一个关于班级的类MyClass,里面有一个list用来记录班级里的所有学生,假设学生类已经定义为Student,那么MyClass的定义大致如下:

public class MyClass

{

private List<Student> allStudents;

//其他字段方法

//....

}

现在想为MyClass定义一个操作,因为学生都是有学号的,所以我想直接用学号来得到某个具体学生的信息,当然了,你可以设计一个public的方法,就像下面一样:

public class MyClass

{

private List<Student> allStudents;

public Student GetStudentByID(int studentID)

{

        if(allStudents==null) return null;

        for(int i=0;i<allStudents.Count;i++)

        {

                if(allStudents[i].id==studentID)

                {

                        return allStudents[i];

                }

        }

        return null;

}

//其他字段方法

//....

}

当然了,使用方法就是:

MyClass mc=new MyClass();

Student st=mc.GetStudentByID(123);

而除了这种添加public函数的方法,索引器也能做到相应的功能,比如我设计一个索引器如下:

public class MyClass

{

private List<Student> allStudents;

public this[int studentID]

{

        get

        {

                if(allStudents==null) return null;

                for(int i=0;i<allStudents.Count;i++)

                {

                        if(allStudents[i].id==studentID)

                        {

                                return allStudents[i];

                        }

                }

                return null;

        }

}

//其他字段方法

//....

}

 

而使用方法就是:

MyClass mc=new MyClass();

Student st=mc[123];

总结一下,索引器其实就是利用索引(index)找出具体数据的方法(函数),在类的设计中它并不是必须的,它是可以用普通函数替代的,简单点理解,其实就是函数的一种特殊写法而已

 

学习系列之索引器与属性

标签:ati   selected   lis   data   记录   nts   str   com   查看   

原文地址:http://www.cnblogs.com/wyh19941210/p/7757652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!