0

Is there any difference in performance between returning a List<String> or returning a StringList? where StringList would be defined as

class StringList : List<String> { }

This would also count for any other defined list such as IntList or ObjectList or ControlList or MyCustomClassList, all defined in a similar way.

The only difference i have seen is readability within the code, would any performance differences be optimised away by the compiler?

Secondly, is there a better way to define StringList or is this way the only way?

bizzehdee
  • 20,289
  • 11
  • 46
  • 76
  • http://stackoverflow.com/questions/5376203/inherit-listt?answertab=votes#tab-top – Habib May 13 '13 at 11:41
  • Did you **notice** any performance impact? I guess not. `List` is clear and readable. There's no need to have a custom class for each bit of code. – Ondrej Tucny May 13 '13 at 11:41
  • Would you also work with `Int32List` or `DoubleList` or `DateTimeBooleanTupleList`? To me `List` or `List` or `List>` would be more readable. – Corak May 13 '13 at 11:41
  • Can you explain a little bit more? Why you need youre own List. I would recommend you to implement IList but not List if you need to add few more usefull features to your inheriter – Maris May 13 '13 at 11:41
  • What you describe would be exactly the same object. It only adds confusion. – Security Hound May 13 '13 at 12:12

3 Answers3

5

Are you going to extend the functionality of List<string>, i.e. add methods, override methods? If not, there's no point of adding this new class.

And I am not sure about the readability - if someone see List<string> he is sure that this is generic list of strings and he knows what to expect. But he can't be sure what StringList class does, he will have to dive into the source code and that means using Reflector or such tool when the source code is not available.

Karel Frajták
  • 4,389
  • 1
  • 23
  • 34
  • 3
    +1, my thoughts exactly. `List` is pretty self-explanatory and standard across all C# code that uses it. On seeing `StringList` I would wonder what the hell it was and why it was different. – Moo-Juice May 13 '13 at 11:44
3

There should be no bigger difference in performance in your case.

A better way to define StringList would be something like

using StringList = System.Collections.Generic.List<string>;

as it makes more clear that you do not want to make additions to that definition. Making a class / using inheritance is a good way if you want to extend something.

But, I still recommend to use List<string>, as every single C# programmer will know what it is. Having custom defines for every possible datatype makes code unnecessary hard to read.

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
0

It only will make a sence when you try to make a implementation of a generic List of your some custom class for adding new functionality and overriding some common features. But I would recommend to implement the IList<> not the List<>. Making new class which implements List of String doesn't makes sence.

Maris
  • 4,608
  • 6
  • 39
  • 68