C#索引器与类列表 [英] C# Indexer versus List of Classes
本文介绍了C#索引器与类列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿,我遇到了C#中的一个新概念,我很难找到索引器的用途.我知道索引器是对诸如数组之类的类进行索引的一种方法,但是与通用(或非常规)类列表相比,索引器的优点是什么?
Hey guys, Ive come accross a new concept in C# that I am having a hard time finding a use for - Indexers. I know that an indexer is a way to index classes like arrays, but what is the advantage of an indexer over a generic (or not) list of classes?
Could someone elaborate on some uses/applications of an indexer?
推荐答案
一个词:索引器更抽象抽象.在许多情况下,后面没有任何容器-属性是动态计算的.我广泛使用此功能;与"FindElementByName
"或"GetElementByIndex
"之类的东西相比,您可以将其视为整洁的语法糖果.
您可以在我的答案之一中的代码示例中找到有趣的应用程序:
数组中引用对象的位置? [ ^ ].
—SA
One word: indexer is more abstract. There are many cases when there is no any container behind — the property is calculated on the fly. I widely use this feature; you can consider it as a neat syntax candy, compared to something like "FindElementByName
" or "GetElementByIndex
".
You can find interesting application in my code sample in one of my answers here:
Location of a refernced object in an array?[^].
—SA
这篇关于C#索引器与类列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文