关于C#索引器.docx
《关于C#索引器.docx》由会员分享,可在线阅读,更多相关《关于C#索引器.docx(13页珍藏版)》请在冰豆网上搜索。
![关于C#索引器.docx](https://file1.bdocx.com/fileroot1/2023-2/2/d1ac8b38-4368-4ada-ae52-79ee4da5b72b/d1ac8b38-4368-4ada-ae52-79ee4da5b72b1.gif)
关于C#索引器
索引器允许类和结构的实例按照与数组相同的方式进行索引,索引器类似于属性,不同之处在于他们的访问器采用参数。
被称为有参属性。
简单的索引器实例:
classProgram
{
staticvoidMain(string[]args)
{
IndexClassa=newIndexClass();
a[0]="陈三";
a[1]="戴四";
a[2]="笠五";
Console.WriteLine("a[0]="+a[0]);
Console.WriteLine("a[1]="+a[1]);
Console.WriteLine("a[2]="+a[2]);
Console.ReadKey();
}
}
classIndexClass
{
privatestring[]name=newstring[10];
publicstringthis[intindex]
{
get{returnname[index];}
set{this.name[index]=value;}
}
}
索引器与数组的比较:
索引器的索引值不受类型限制。
用来访问数组的索引值一定是整数,而索引器可以是其他类型的索引值。
索引器允许重载,一个类可以有多个索引器。
索引器不是一个变量没有直接对应的数据存储地方。
索引器有get和set访问器。
索引器允许类和结构的实例按照与数组相同的方式进行索引,索引器类似与属性,不同之处在于他们的访问器采用参数。
被称为有参属性。
简单的索引器实例:
索引器与属性的比较:
标示方式:
属性以名称来标识,索引器以函数签名来标识。
索引器可以被重载。
属性则不可以被重载。
属性可以为静态的,索引器属于实例成员,不能被声明为static
多参数索引器实例:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Collections;
namespaceStudy
{
classProgram
{
staticvoidMain(string[]args)
{
ScoreIndexs=newScoreIndex();
s["张三",1]=90;
s["张三",2]=100;
s["张三",3]=80;
s["李四",1]=60;
s["李四",2]=70;
s["李四",3]=50;
Console.WriteLine("张三课程编号为1的成绩为:
"+s["张三",1]);
Console.WriteLine("张三的所有成绩为:
");
ArrayListtemp;
temp=s["张三"];
foreach(IndexClassbintemp)
{
Console.WriteLine("姓名:
"+b.Name+"课程编号:
"+b.CourseID+"分数:
"+b.Score);
}
Console.ReadKey();
}
}
classIndexClass
{
privatestring_name;
privateint_courseid;
privateint_score;
publicIndexClass(string_name,int_courseid,int_score)
{
this._name=_name;
this._courseid=_courseid;
this._score=_score;
}
publicstringName
{
get{return_name;}
set{this._name=value;}
}
publicintCourseID
{
get{return_courseid;}
set{this._courseid=value;}
}
publicintScore
{
get{return_score;}
set{this._score=value;}
}
}
classScoreIndex
{
privateArrayListarr;
publicScoreIndex()
{
arr=newArrayList();
}
publicintthis[string_name,int_courseid]
{
get
{
foreach(IndexClassainarr)
{
if(a.Name==_name&&a.CourseID==_courseid)
{
returna.Score;
}
}
return-1;
}
set
{
arr.Add(newIndexClass(_name,_courseid,value));//arr["张三",1]=90
}
}
//重载索引器
publicArrayListthis[string_name]
{
get
{
ArrayListtemp=newArrayList();
foreach(IndexClassbinarr)
{
if(b.Name==_name)
{
temp.Add(b);
}
}
returntemp;
}
}
}
}
备注:
所有索引器都使用this关键词来取代方法名。
Class或Struct只允许定义一个索引器,而且总是命名为this。
索引器允许类或结构的实例按照与数组相同的方式进行索引。
索引器类似于属性,不同之处在于它们的访问器采用参数。
get访问器返回值。
set访问器分配值。
this关键字用于定义索引器。
value关键字用于定义由set索引器分配的值。
索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
索引器可被重载。
索引器可以有多个形参,例如当访问二维数组时。
索引器可以使用百数值下标,而数组只能使用整数下标:
如下列定义一个String下标的索引器
publicintthis[stringname]{...}
属性和索引器
属性和索引器之间有好些差别:
类的每一个属性都必须拥有唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名(signature)或者参数列表(这样就可以实现索引器重载)。
属性可以是static(静态的)而索引器则必须是实例成员。
摘自红色黑客联盟()原文:
C#索引器语法:
[访问修饰符]数据类型this[数据类型标识符]。
示例1:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespacePH6_2
{
classPhoto
{
string_title;
publicPhoto(stringtitle)
{
_title=title;
}
publicstringTitle
{
get
{
return_title;
}
set
{
_title=value;
}
}
}
classAlbum
{
Photo[]photos;
publicAlbum(intcapacity)
{
photos=newPhoto[capacity];
}
publicPhotothis[intindex]
{
get
{
if(index<0||index>=photos.Length)
{
Console.WriteLine("索引无效");
returnnull;
}
returnphotos[index];
}
set
{
if(index<0||index>=photos.Length)
{
Console.WriteLine("索引无效");
return;
}
photos[index]=value;
}
}
publicPhotothis[stringtitle]
{
get
{
foreach(Photophotoinphotos)
{
if(photo.Title==title)
{
returnphoto;
}
}
returnnull;
}
}
}
classTestIndex
{
staticvoidMain(string[]args)
{
Photofirst=newPhoto("first");
Photosecond=newPhoto("second");
Photothird=newPhoto("third");
Albumalbum=newAlbum(3);
album[0]=first;
album[1]=second;
album[2]=third;
Console.WriteLine("第2张照片:
"+album[1].Title);
Console.WriteLine(album["third"].Title);
}
}
}
运行效果如下:
注意,不能定义两个相同数据类型的索引器,但是可以定义不同数据类型的索引器,其返回值可以相同。
例如下面的示例是不正确的:
两个索引器,索引都是int,编译报错:
类型“PH6_2.TestIndex”已定义了一个名为“this”的具有相同参数类型的成员。
classTestIndex
{
int[]num1s={1,2,3,4,5};
string[]s={"a","b","c"};
publicintthis[intindex]
{
get
{
returnnum1s[index];
}
}
publicstringthis[intindex]
{
get
{
returns[index];
}
}
staticvoidMain(string[]args)
{
TestIndextestIndex=newTestIndex();
uinti=2;
Console.WriteLine(testIndex[i]);
}
}
可以将第2个索引器的索引改为uint,则编译通过。
如下:
classTestIndex
{
int[]num1s={1,2,3,4,5};
string[]s={"a","b","c"};
publicintthis[intindex]
{
get
{
returnnum1s[index];
}
}
publicstringthis[uintindex]
{
get
{
returns[index];
}
}
staticvoidMain(string[]args)
{
TestIndextestIndex=newTestIndex();
uinti=2;
Console.WriteLine(testIndex[i]);
}
}
还需要注意:
索引的索引类型不同,而返回值相同,也是正确的。
如下示例是正确的:
classTestIndex
{
int[]num1s={1,2,3,4,5};
int[]num2s={10,11,12,13,14,15};
publicintthis[intindex]
{
get
{
returnnum1s[index];
}
}
publicintthis[uintindex]
{
get
{
returnnum2s[index];
}
}
staticvoidMain(string[]args)
{
TestIndextestIndex=newTestIndex();
uinti=2;
Console.WriteLine(testIndex[i]);
Console.WriteLine(testIndex[2]);
}
}
本文来自CSDN博客,转载请标明出处: