C# 索引器使用總結
1、索引器(Indexer):
索引器允許類或者結構的例項按照與陣列相同的方式進行索引。索引器類似於屬性,不同之處在於他們的訪問採用引數。
最簡單的索引器的使用
程式碼如下:
/// <summary>
/// 最簡單的索引器
/// </summary>
public class IDXer
{
private string[] name=new string[10];
//索引器必須以this關鍵字定義,其實這個this就是類例項化之後的物件
public string this[int index]
{
get
{
return name[index];
}
set
{
name[index] = value;
}
}
}
public class Program
{
static void Main(string[] args)
{
//最簡單索引器的使用
IDXer indexer = new IDXer();
//“=”號右邊對索引器賦值,其實就是呼叫其set方法
indexer[0] = "張三";
indexer[1] = "李四";
//輸出索引器的值,其實就是呼叫其get方法
Console.WriteLine(indexer[0]);
Console.WriteLine(indexer[1]);
Console.ReadKey();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2、索引器與陣列的區別:
索引器的索引值(Index)型別不限定為整數:
用來訪問陣列的索引值(Index)一定為整數,而索引器的索引值型別可以定義為其他型別。
索引器允許過載
一個類不限定為只能定義一個索引器,只要索引器的函式簽名不同,就可以定義多個索引器,可以過載它的功能。
索引器不是一個變數
索引器沒有直接定義資料儲存的地方,而陣列有。索引器具有Get和Set訪問器。
3、索引器與屬性的區別:
索引器以函式簽名方式 this 來標識,而屬性採用名稱來標識,名稱可以任意
索引器可以過載,而屬性不能過載。
索引器不能用static 來進行宣告,而屬性可以。索引器永遠屬於例項成員,因此不能宣告為static。
以字串作為下標,對索引器進行存取:
//以字串為下標的索引器
public class IDXer2
{
private Hashtable name = new Hashtable();
//以字串為下標的索引器
public string this[string index]
{
get
{
return name[index].ToString();
}
set
{
name.Add(index, value);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Program
{
static void Main(string[] args)
{
//以字串為下標的索引器
IDXer2 indexer2 = new IDXer2();
indexer2["A01"] = "張三";
indexer2["A02"] = "李四";
Console.WriteLine(indexer2["A01"]);
Console.WriteLine(indexer2["A02"]);
Console.ReadKey();
}
1
2
3
4
5
6
7
8
9
10
11
12
}
多引數索引器及索引器的過載
/// <summary>
/// 成績類
/// </summary>
public class Scores
{
/// <summary>
/// 學生姓名
/// </summary>
public string StuName { get; set; }
/// <summary>
/// 課程ID
/// </summary>
public int CourseId { get; set; }
/// <summary>
/// 分數
/// </summary>
public int Score { get; set; }
}
/// <summary>
/// 查詢成績類(索引器)
/// </summary>
public class FindScore
{
private List<Scores> listScores;
public FindScore()
{
listScores = new List<Scores>();
}
//索引器 通過名字&課程編號查詢和儲存成績
public int this[string stuName, int courseId]
{
get
{
Scores s = listScores.Find(x => x.StuName == stuName && x.CourseId == courseId);
if (s != null)
{
return s.Score;
}
else
{
return -1;
}
}
set
{
listScores.Add(new Scores() { StuName = stuName, CourseId = courseId, Score = value });
}
}
//索引器過載,根據名字查詢所有成績
public List<Scores> this[string stuName]
{
get
{
List<Scores> tempList = listScores.FindAll(x => x.StuName == stuName);
return tempList;
}
}
}
static void Main(string[] args)
{
//多引數索引器和索引器過載
FindScore fScore = new FindScore();
fScore["張三", 1] = 98;
fScore["張三", 2] = 100;
fScore["張三", 3] = 95;
fScore["李四", 1] = 96;
//查詢 張三 課程編號2 的成績
Console.WriteLine("李四 課程編號2 成績為:" + fScore["李四", 1]);
//查詢所有張三的成績
List<Scores> listScores = fScore["張三"];
if (listScores.Count > 0)
{
foreach (Scores s in listScores)
{
Console.WriteLine(string.Format("張三 課程編號{0} 成績為:{1}", s.CourseId, s.Score));
}
}
else
{
Console.WriteLine("無該學生成績單");
}
Console.ReadKey();
}
---------------------
作者:望崖
來源:CSDN
原文:https://blog.csdn.net/kingscoming/article/details/78847060
版權宣告:本文為博主原創文章,轉載請附上博文連結!