C#语言中Index属性的优点和缺点是什么? [英] What is the merit and demerit of Index property in C# language?

查看:165
本文介绍了C#语言中Index属性的优点和缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,C#中有索引属性,就像这样使用。

  public  CardType < span class =code-keyword> this  [ int  index] {
get { return cards [index]; }
set {cards [index] = value ; }
}



好​​吧,我认为它非常有用但有人说它没用,而且使用它有很多缺点让我现在感到困惑。

有谁可以告诉我索引属性的优点和缺点是什么?我等你有价值的答案。

解决方案

索引允许你使用代表类似数组的集合的类几种不同的东西。阅读本讨论后,您可以找到一些缺点:

为什么C#没有实现索引属性? [ ^ ]


好吧,在我们讨论了提出这些问题的优点(:-))之后,我可以分享一些更重要的东西,仅举例来说。



想象一下,你有一些巨大的文件。文件的大小可能超过可用内存甚至可寻址空间(在32位系统中很可能)。您可以将此文件表示为索引可访问的对象集合。方法如下:

  class  FileArray {

// ...

string [ ulong index] { // 我们需要一个非常长的索引整数类型
get {
< span class =code-keyword> ulong position = // ...读取地址表
// 按索引检索记录的位置
// 将文件中的位置设置为上一步计算的值
int recordLength = // ...读取记录的长度
// 从文件读取长度字节
string value = // ...根据使用的编码将字节反序列化为字符串
return value ;
}
}

}





这样,你可以隐藏一个漂亮的处理简单索引接口后面的一些大文件的复杂算法,这个类外面唯一可见的东西。如果愿意,您可以将其视为外观模式的变体之一:

http://en.wikipedia.org/wiki/Facade_pattern [ ^ ]。



另一个有趣的话题:有些人认为索引器的限制只有每种类型一个。这有时被认为是具有.NET中不可用的索引属性的参数。我想在.NET中也有索引属性(请参阅我对解决方案1的评论),但在.NET中,索引器的这种限制并非如此:解决方法很简单。请看我过去的答案:

引用对象在数组中的位置? [ ^ ]。



我看到索引器中没有缺点,我只是在没有索引属性的情况下看到一些缺点,解决方案1中提供的链接对此进行了解释。



-SA

Well, there is index property in C# which is used like this.

public CardType this[ int index] {
          get { return cards[ index ]; }
          set { cards[ index ]= value; }
   }


Well, I think it is very useful but somebody said it is not useful and there are many disadvantage of using it so I'm now puzzled.
Could anybody tell me what is the merit and demerit of index property? I wait for you valuable answer.

解决方案

Index allow you to use a class that represents an array-like collection of several different kinds of things. You can find some demerits after reading this discussion:
Why C# doesn't implement indexed properties?[^]


All right, after we discussed the "merit" of asking such questions (:-)), I can share something more essential, just for example.

Imagine you have a huge file of some objects. The size of the file could be more than available memory or even addressable space (it would be quite possible in 32-bit systems). You can represent this file as the collection of objects accessible by index. Here is how:

class FileArray {

//...

    string this[ulong index] { // we need a really long integer type for the index
        get {
           ulong position = //... read the address table
                            // to retrieve the position of the record by index
           // set position in the file to the value calculated in the last step
           int recordLength = //... read the length of the record 
           // read length bytes from the file
           string value = //... deserialize bytes into string according the encoding used
           return value;
        }
    }

}



This way, you can hide a pretty complex algorithm of dealing with some big file behind a simple indexed interface, the only thing visible from outside of this class. If you will, you can consider it as one of the variants of the facade pattern:
http://en.wikipedia.org/wiki/Facade_pattern[^].

One more interesting topics: some think that the limitation of indexers is having only one per type. This is sometimes considered as the argument for having indexed properties which are not available in .NET. I would like to have indexed properties in .NET as well (please see my comments to Solution 1), but this limitation of indexers is not the case in .NET: the workaround is easy. Please see my past answer:
Location of a refernced object in an array?[^].

And I see no "demerits" in indexers, I only see some "demerit" in absence of indexed properties, which are explained in the link provided in Solution 1.

—SA


这篇关于C#语言中Index属性的优点和缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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