C# typedef

Has anyone else found themselves doing the following:

      public class FooList : System.Collections.Generic.List<Foo>


Rather than just using or exposing the List<Foo> type directly?  For some reason I prefer the strongly type name.  True, I would most likely expand on this class for such things as an overloaded Add() method, etc. Anyone see anything wrong with this? Will you end up with too many (trivial?) classes to maintain?

Comments (9)
  1. ShadowChaser says:

    I wondered the exact same thing, honestly 🙂

    Generics are not CLS compliant (DOH!). As a result, I’ve been using the following design approach:

    If the collection is exposed to a consumer outside of my application (ie/ I’m writing a component, plugin app, etc), I always use the standard collections. What i’m not sure about, however, is if wrapping a class around a generic collection makes it CLS compliant. Hopefully it does!

    If the collection is only used internally or privately, I expose the ‘generic list’ directly.

    Part of the .NET naming approach/guidelines is to have "human readable" type and member names. If I was developing a component (even if it was only used by C#), I would probably wrap them with a nice name (as you demonstrated).

    I also like expanding some of my custom collections to have special Item/IndexOf/Contains members when it makes sense.

    Any idea what the difference between System.Collections.Generic.List and System.Collections.Generic.Collection are?

  2. I don’t think this is as necessary as it was in C++. Judging by your "typedef" headline I’d say your habits are also C++ heritage. However, in C++ I used to typedef in a way similar to above because there were inner types I needed (think iterators) and I did not want to type GenericType<SomePotentiallyVeryLongName>::iterator where GenericFoo::iterator sufficed. This does not seem to be practice with C# so I don’t envision the need.

  3. Hamza GOLYERI says:

    I think exposing strongly typed collections can be useful (even sometimes necessary) for providing strongly types APIs. It totally depends on the requirements of API you are exposing and I think it is not possible say something general whether it would be appropriate or not.

    For any reason if you prefer to use generic collection internally with ‘strongly typed’ names (or more ‘human readable’ names) I guess you can use the ‘using’ directive as:

    using FooList = System.Collections.Generic.List<Foo>;

  4. The thing is that in C++ as with C#, generic types are still strongly typed. And all of their methods are as well. So if you are worried about strong typing then you don’t need to do what you are doing.

  5. Mitch Walker says:

    No, I’m not worried about compile time or runtime issues, this is merely a cosmetic thing.

  6. I Got Rhythm says:

    I’ve just seen a blog entry about C# generics, concerning C#’s lack of typedef s. Actually, C# has a

  7. I Got Rhythm says:

    I&#39;ve just seen a blog entry about C# generics, concerning C#&#39;s lack of typedef s. Actually, C#

Comments are closed.

Skip to main content