夜光带你走进C#语言 中级版本(4)

阅读: 评论:0

夜光带你走进C#语言 中级版本(4)

夜光带你走进C#语言 中级版本(4)

夜光序言:

 

 

不要轻易去依赖一个人,它会成为你的习惯,当分别来临,你失去的不是某个人,而是你精神的支柱。无论何时何地,都要学会独立行走,它会让你走得更坦然些。

 

 

 

 

 

 

 

 

 

正文:

C# 索引器(Indexer)

索引器(Indexer) 允许一个对象可以像数组一样被索引。

 

当我们为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。

 

我们可以使用数组访问运算符([ ])来访问该类的实例。

语法

一维索引器的语法如下:

element-type this[int index]
{
   // get 访问器
   get
   {
      // 返回 index 指定的值
   }

   // set 访问器
   set
   {
      // 设置 index 指定的值
   }
}

索引器(Indexer)的用途

索引器的行为的声明在某种程度上类似于属性(property)。

就像属性(property),我们可使用 getset 访问器来定义索引器。

但是,属性返回或设置一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。

 

换句话说,它把实例数据分为更小的部分,并索引每个部分,获取或设置每个部分。

 

定义一个属性(property)包括提供属性名称。索引器定义的时候不带有名称,但带有 this 关键字,它指向对象实例。

 

下面的实例演示了这个概念:

实例

 

using System;namespace GeniusTest01
{class IndexedNames{private string[] namelist = new string[size];static public int size = 10;public IndexedNames(){for (int i = 0; i < size; i++)namelist[i] = "N. A.";}public string this[int index]{get{string tmp;if( index >= 0 && index <= size-1 ){tmp = namelist[index];}else{tmp = "";}return ( tmp );}set{if( index >= 0 && index <= size-1 ){namelist[index] = value;}}}public class Class116{static void Main(string[] args){IndexedNames names = new IndexedNames();names[0] = "A斗罗大陆";names[1] = "B斗破苍穹";names[2] = "C绝世唐门";names[3] = "D武动乾坤";names[4] = "E";names[5] = "F";names[6] = "G";for (int i = 0; i < IndexedNames.size; i++){Console.WriteLine(names[i]);}Console.ReadKey();}}}
}

 

 

 

 

 

 

 

 

 

 

重载索引器(Indexer)

索引器(Indexer)可被重载。索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。

 

没有必要让索引器必须是整型的。

 

C# 允许索引器可以是其他类型,例如,字符串类型。

下面的实例演示了重载索引器:

 

实例

using System;namespace GeniusTest01
{class IndexedNames{private string[] namelist = new string[size];static public int size = 10;public IndexedNames(){for (int i = 0; i < size; i++){namelist[i] = "N. A.";}}public string this[int index]{get{string tmp;if( index >= 0 && index <= size-1 ){tmp = namelist[index];}else{tmp = "";}return ( tmp );}set{if( index >= 0 && index <= size-1 ){namelist[index] = value;}}}public int this[string name]{get{int index = 0;while(index < size){if (namelist[index] == name){return index;}index++;}return index;}}public class Class117{static void Main(string[] args){IndexedNames names = new IndexedNames();names[0] = "A斗罗大陆";names[1] = "B斗破苍穹";names[2] = "C绝世唐门";names[3] = "D武动乾坤";names[4] = "E";names[5] = "F";names[6] = "G";// 使用带有 int 参数的第一个索引器for (int i = 0; i < IndexedNames.size; i++){Console.WriteLine(names[i]);}// 使用带有 string 参数的第二个索引器Console.WriteLine(names["Nuha"]);Console.ReadKey();}}}
}

 

 

 

/bin/Debug/netcoreapp2.2/GeniusTest01.dll
A斗罗大陆
B斗破苍穹
C绝世唐门
D武动乾坤
E
F
G
N. A.
N. A.
N. A.
10

 

本文发布于:2024-01-30 21:11:13,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170662027622869.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:带你   语言   版本
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23