Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
2.65/5 (3 votes)
See more:
Well, there is index property in C# which is used like this.
C#
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.
Posted
Updated 8-Dec-13 19:31pm
v2
Comments
Mehdi Gholam 9-Dec-13 1:31am    
It's a language feature, and it's merits depend on your usage.
WuRunZhe 9-Dec-13 1:37am    
Yes, you're right. But if you have to answer that question to another people, how could you explain it?
Sergey Alexandrovich Kryukov 9-Dec-13 1:46am    
People don't answer invalid or just vague questions. And not answering them, in a way, is even more important than answering. Why multiplying the amount of lie in the worlds? (And please pay attention for my comment to your previous comment. You certainly need to think about it).

As to your concern, the only problem is to clarify what exactly do you need. You might: 1) have doubt about syntax, 2) about how it works, 3) have certain problems with design of some code, 4) something else....

Only when you clarify it, there will be some sense in answering. You should understand that "merit" does not really have strict sense. Do you see the point?

Besides, all you need it to read original MSDN documentation.

—SA
WuRunZhe 9-Dec-13 2:10am    
Well, you said I have to clarify what I need and split it in 4 types. The fourth is "4) something else...". Am I right? My question belongs in fourth. Are you clear?
Sergey Alexandrovich Kryukov 9-Dec-13 2:14am    
I really appreciate your sense of humor. Now it's reduced to clarification on #4... :-)
—SA

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?[^]
 
Share this answer
 
Comments
WuRunZhe 9-Dec-13 2:24am    
Thank you, but I think it is not simple answer. I'm not good at english, so I could extract the demerit from your answer.
ridoy 9-Dec-13 2:34am    
Well, Demerits are lesser part of this topic.But some demerits could be noise and ambiguity.Indexed properties would mean you don't have a child collection instance. That's both good and bad. It's less trouble to implement and you don't need a reference back to the enclosing owner class. But it also means you can't pass that child collection to anything; you'd likely have to enumerate every single time. Nor can you do a foreach on it. Worst of all, you can't tell from looking at an indexed property whether it's that or a collection property.
Sergey Alexandrovich Kryukov 9-Dec-13 2:48am    
We should understand that "demerit" cannot have any strict sense. I would prefer not to discuss it. It would be pretty useless, unless some real problem is discussed. Besides, the answer does not discuss any "demerits" of "this" indexer (and I think this is good). The link discusses the merit of the feature which is completely absent from .NET: indexed properties. I see no "demerit" in them, and on that basis criticized the answer, which is not a big problem of it.
—SA
Sergey Alexandrovich Kryukov 9-Dec-13 2:33am    
I voted 5, but I think the "demerit" part (absolutely vague topic due to its nature) and the link is quite biased. I do understand that it was "just for your information" or something like that.

Here is what I think: there is nothing wrong with having indexed properties (not just "this" indexer). Delphi, the predecessor of .NET, had them. I would personally vote for having them. The arguments against them are not sound at all. We can leave without them, but they could be quite helpful...

This is not so principle issue...

—SA
ridoy 9-Dec-13 2:38am    
Agree, not so principle regards to it's merits. Moreover, we had other options too to avoid index.
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:
C#
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
 
Share this answer
 
v2
Comments
BillWoodruff 9-Dec-13 3:44am    
+5 A very interesting, thoughtful, response to a strange question.
Sergey Alexandrovich Kryukov 9-Dec-13 9:51am    
Thank you very much, Bill.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900