什么是索引器? [英] What Is an Indexer?

查看:97
本文介绍了什么是索引器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道waht是一个索引以及我们如何使用它与一个简单的例子

包括程序的主要部分。


thanx

-

Islam Khalil,

i want to know waht is an index and how we use it with a simple example
including the main of the program .

thanx
--
Islam Khalil,

推荐答案

SemSem写道:
我想知道waht是一个索引,我们如何使用它包含一个简单的例子,包括程序的主要部分。
i want to know waht is an index and how we use it with a simple
example including the main of the program .




索引器是伪的-property允许像

数组一样访问对象。


< code>

使用System;


公共类示例

{

int this [int index]

{

get {return index; }

set {Console.WriteLine(" Set:{0}",value); }

}


static void Main()

{

示例e =新示例( );

Console.WriteLine(" e [5] = {0}",e [5]);

e [6] = 42;

}

}


< / code>


这当然是一个完全做作和无用的例子,但是它表示如何在Main()中访问对象e,好像它是一个数组

的整数。请注意,索引器的getter和setter不需要实现甚至远离数组的行为,尽管

一般来说这是个好主意只使用索引器才能获得真正类似于数组的

行为。


注意一个对象可以有任意数量的索引器,并且索引

的伪数组可以有任何类型,并且可以有任意数量:


< code>

使用系统;


公共类例2

{

string this [string s1,string s2,string s3]

{

get {return string.Format(" {0}:{1}:{2}",s1,s2,s3); }

set {Console.WriteLine(" {0}:{1}:{2} = {3}",s1,s2,s3,value); }

}


static void Main()

{

示例2 e2 =新示例2( );

Console.WriteLine(" {0}",e2 [" one",two,three]);

e2 [" hello"," world"," !!"] ="哇!" ;;

}

}

< / code>


-cd



An indexer is a pseudo-property that allows an object to be accessed like an
array.

<code>
using System;

public class Example
{
int this[int index]
{
get { return index; }
set { Console.WriteLine("Set: {0}",value); }
}

static void Main()
{
Example e = new Example();
Console.WriteLine("e[5]={0}",e[5]);
e[6] = 42;
}
}

</code>

This is, of course, a completely contrived and useless example, but it
illustrates how, in Main(), the object e is accessed as if it were an array
of integers. Note that the getter and setter for the indexer need not
implement behavior that''s even remotely like that of an array, although
generally it''s a good idea to only use an indexer for truly array-like
behavior.

Note that an object can have any number of indexers, and that the "indexes"
of the pseudo-array can have any type and there can be any number of them:

<code>
using System;

public class Example2
{
string this[string s1, string s2, string s3]
{
get { return string.Format("{0}:{1}:{2}",s1,s2,s3); }
set { Console.WriteLine("{0}:{1}:{2}={3}",s1,s2,s3,value ); }
}

static void Main()
{
Example2 e2 = new Example2();
Console.WriteLine("{0}",e2["one","two","three"]);
e2["hello","world","!!"] = "Wow!";
}
}
</code>

-cd


" SemSem" <硒**** @ discussions.microsoft.com>在消息中写道

新闻:CB ********************************** @ microsof t.com ...
"SemSem" <Se****@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
我想知道waht是一个索引以及我们如何使用它包含一个简单的例子
包括程序的主要部分。
i want to know waht is an index and how we use it with a simple example
including the main of the program .




你应该改变专业。



You should change majors.


哇,你真的很有帮助。你真的是那种应该回答这里发布的问题的人。


感谢您的宝贵帮助!

Mark Wilden <马******** @ newsgroups.nospam>在消息中写道

news:uE **************** @ TK2MSFTNGP05.phx.gbl ...
Wow, you are so helpful. You really are the type of person that should be
responding to questions posted here.

Thanks for all your invaluable help!
"Mark Wilden" <Ma********@newsgroups.nospam> wrote in message
news:uE****************@TK2MSFTNGP05.phx.gbl...
" SemSem" ; <硒**** @ discussions.microsoft.com>在消息中写道
新闻:CB ********************************** @ microsof t.com。 ..
"SemSem" <Se****@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
我想知道waht是一个索引以及我们如何使用它包含一个简单的例子
包括程序的主要部分。
i want to know waht is an index and how we use it with a simple example
including the main of the program .



你应该改变专业。



You should change majors.



这篇关于什么是索引器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆