Why do you get a List <T> class to recalculate an index?

I continue to see classes with List derivatives that look something like this.

class MyClassList : List<MyClass> { public MyClass this[int index] { get { return (MyClass)base[index]; } } } 

What is the meaning of this inheritance? Looks like he just repeats the casting of the participant. I could understand other types of indexers, but this is just a repetition of the default index indexer and provokes a Visual Studio RE warning: hiding the underlying indexer. Is this the right or wrong thing, and why?

+4
source share
7 answers

There is no good reason for this. It hides the underlying index, instead of overriding it, which can be dangerous, and it has no effect at all.

In most cases, it is best to use List<MyClass> directly. There is no need to create a special class for it, if you do not plan to extend the functionality of List<> .

+2
source

Perhaps this is a very bad attempt to prevent rewriting of values ​​with an indexer?

 MyClassList x = new MyClassList(); x.Add(new MyClass()); x[0] = new MyClass(); // Error! 

Of course, this does not stop it:

 List<MyClass> x = new MyClassList(); x.Add(new MyClass()); x[0] = new MyClass(); // No problem here... 

Basically, this is a bad idea. Bad code abounds, unfortunately - it does not infer usefulness from simple existence :(

+5
source

I see no use for this. In addition, a throw is not needed.

+1
source

I think it is supposed to hide the set accessory of the base class so that it looks as if the indexer is read-only. But this is useless because it is very easy to get around:

 MyClassList list = ... ((List<MyClass>)list)[index] = value; 

In any case, the List<T> class is not intended to be inherited. If you need to create a custom collection, inherit it from Collection<T> .

+1
source

I saw this type of material before people want to serialize it as another type, but for now, if this is the only code and it is not required for the MyClassList type for other reasons, it is completely pointless.

0
source

this is basically an attempt to correctly redefine [] access to List to implement some logic of access to individual elements.

It is worth noting that the code provided is not good, if not just dangerous. If you want to do something complicated with a list, do not redefine (or are not inclined to do this) [] , but implement some kind of custom method for this.

0
source

I assume the code tried to behave as a read-only List . It is not possible to write an element of a variable of type MyClassList<T> by index, although you could return it back to List<T> and write the variable in this way. There are times when it makes sense to have a variable whose type has limited capabilities, to hold an object whose actual capabilities are much larger. However, the correct way to do this is usually with interfaces, the main example of which is IEnumerable<T> . If a List<T> is passed to a routine that accepts a parameter of type IEnumerable<T> , the routine can return its parameter back to List<T> and use elements such as Add() , Remove() , etc., but should Performing procedures that take a parameter of type IEnumerable<T> will not try to use it as anything else.

The main problem with the code style shown by the original poster is that the more β€œpowerful” direction is the base, not the derived type. Since List<T> comes from IEnumerable<T> , this means that all instances of List<T> can be listed, but not only some enumerated things have additional features in List<T> . In contrast, as your class implements, each MyClassList<T> can be read and written, but only some instances of List<T> can be used as MyClassList<T> .

0
source

Source: https://habr.com/ru/post/1402249/


All Articles