C#索引器

来源:百度知道 编辑:UC知道 时间:2024/06/07 21:03:04
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;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
telephoneList telephoneList = new telephoneList();
telephoneList.initialize();
this.textBox1.Text = telephoneList[11000];
}
}
public class Student
{
public string name;
public int ID;
}
public class telephoneList
{
public Student[] Students;
public void initialize()
{
Students = new Studen

get
{
for (int i = 0; i < 3; i++)
{
if (Students[i].ID==theID)
{
return Students[i].name;
}
}
throw new IndexOutOfRangeException();
}

public string this[int theID]
{
get
{
for (int i = 0; i < 3; i++)
{
if (Students[i].ID==theID)
{
return Students[i].name;
}
}
return "";
}
}
get属性必须有返回值

public string this[int theID]
{
get
{
for (int i = 0; i < 3; i++)
{
if (S