简体   繁体   English

C#中需要索引器吗

[英]What is the need Indexers in C#

Today I've gone through what indexers are, but I am bit confused. 今天,我经历了什么是索引器,但我有些困惑。 Is there really a need for indexers? 确实需要索引器吗? What are the advantages of using an indexer..... thanks in advance 使用分度器有什么好处.....在此先感谢

I guess the simplest answer is to look at how you'd use (say) List<T> otherwise. 我猜最简单的答案是看看您如何使用(例如) List<T> Would you rather write: 您愿意写:

 string foo = list[10];

or 要么

 string foo = list.Get(10);

Likewise for dictionaries, would you rather use: 同样,对于字典,您是否愿意使用:

 map["foo"] = "bar";

or 要么

 map.Put("foo", "bar");

?

Just like properties, there's no real need for them compared with just named methods following a convention... but they make code easier to understand, in my view - and that's one of the most important things a feature can do. 就像属性一样,与仅遵循约定的命名方法相比,它们并没有真正的需求 ...但是,在我看来,它们使代码更易于理解-这是功能可以执行的最重要的操作之一。

Indexers let you get a reference to an object in a collection without having to traverse the whole collections. 索引器使您可以引用集合中的对象,而不必遍历整个集合。

Say you have several thousands of objects, and you need the one before last. 假设您有数千个对象,并且您需要一个前一个。 Instead of iterating over all of the items in the collection, you simply use the index of the object you want. 无需遍历集合中的所有项目,您只需使用所需对象的索引即可。

Indexers do no have to be integers, so you can use a string, for example, (though you can use any object, so long as the collection supports it) as an indexer - this lets you "name" objects in a collection for later retrieval, also quite useful. 索引器不必一定是整数,因此您可以使用一个字符串(例如,尽管可以使用任何对象,只要该集合支持它即可)作为索引器-这使您可以“命名”集合中的对象以供以后使用检索,也很有用。

I think zedo got closest to the real reason IMHO that they have added this feature. 我认为zedo最接近他们添加了此功能的真实原因恕我直言。 It's for convenience in the same way that we have properties. 为了方便起见,我们具有属性。

The code is easer to type and easier to read, with a simple abstraction to help you understand. 该代码更易于键入且更易于阅读,并带有简单的抽象以帮助您理解。

For instance: 例如:

string[] array;
string value = array[0];

List<string> list;
string value = list[0];  //Abstracts the list lookup to a call similar to array.

Dictionary<string, int> map;
int value = map["KeyName"]; //Overloaded with string lookup.

索引器允许您以与数组相同的方式引用您的类,这在创建集合类时很有用,但是在其他情况下(例如处理大文件或抽象时),给类提供类似数组的行为也很有用一组有限的资源。

yes , they are very use of 是的,他们非常喜欢

you can use indexers to get the indexed object. 您可以使用索引器来获取索引对象。

Taken from MSDN 取自MSDN

Indexers are most frequently implemented in types whose primary purpose is to encapsulate an internal collection or array. 索引器最经常以其主要目的是封装内部集合或数组的类型实现。

Full Story 全文

for some reason, use indexer can let you create meaningful index to store or map your data. 由于某种原因,使用索引器可以使您创建有意义的索引来存储或映射数据。 then you can get it from other side by the meaningful index. 那么您可以通过有意义的索引从另一端获取它。

using System;

/* Here is a simple program. / *这是一个简单的程序。 I think this will help you to understand */ 我认为这将帮助您了解* /

namespace Indexers
{

    class Demo
    {
        int[] a = new int[10];


        public int Lengths
        {
            get
            {
                return a.Length;
            }
        }


        public int this[int index]
        {
            get
            {
                return a[index];
            }

            set
            {
                a[index] = value;
            }
        }

    }


    class Program
    {
        static void Main(string[] args)
        {

            Demo d = new Demo();  // Notice here, this is a simple object
                                  //but you can use this like an array

            for (int i = 0; i < d.Lengths; i++)
            {
                d[i] = i;
            }

            for (int i = 0; i < d.Lengths; i++)
            {
                Console.WriteLine(d[i]);
            }

             Console.ReadKey();
        }
    }
}

/*Output: 
0
1
2
3
4
5
6
7
8
9
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM