C# - 索引在其应用意义 [英] C# - Indexer at its meaningful application

查看:93
本文介绍了C# - 索引在其应用意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解 索引使我们能够在类中访问集合仿佛类
本身是一个数组

I understand indexer enable us to access a collection within a class as though the class itself were an array.

假设我们正在开发一个项目,我们在哪里符合实际之实践的使用这样的索引器(例如可以帮助)?

Suppose we are developing a project ,where do we fit the real pratical usage (example may help) of such indexers ?

推荐答案

看几乎集合类的任何为例框架。如果我是没有索引检索的语言哈希表中的项目,我会用语法如下:

Look to almost any of the collection classes in the framework for an example. If I were to retrieve an item from a hashtable in a language without an indexer, I would use syntax like:

object o = table.getvalue(key);

其中一个索引可以更简单的语法:

where an indexer allows somewhat simpler syntax:

object o = table[key];



(故意忽略打字/上述样品仿制药的问题)

(purposely ignoring typing/generics issues in the samples above)

这篇关于C# - 索引在其应用意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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