Are templates usefull?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • luke

    Are templates usefull?

    Are they?
    I mean, you could always downcast them for particular use, but then the
    system would be well designed with intefaces instead of generics, and for
    other uses I don't see any sense (collections excluded). Why do I need to
    hold something of type 'object'? For what concrete usage? I am confused with
    interfaces and generics... Please someone explain to me.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Are templates usefull?

    luke,

    Generics are a great way to get general functionality while exposing
    specific types.

    You have already mentioned the example of collections, for example.

    With interfaces and generics, they are a way of specifying the specific
    type to use for the contract that makes up the interface.

    For example, you know that the IEnumerator interface is going to iterate
    through a list/collection/whatever. In order to support all types, it just
    returns an object, which you then have to cast. This isn't type safe, and
    an erroneous cast will be caught at run-time, not compile time.

    With IEnumerator<T>, you don't have to cast, because the Current
    property returns T. If you try to misassign this to another variable, you
    catch it at compile time.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "luke" <luke@hotmail.c om> wrote in message
    news:dr8f2h$4v3 $1@sunce.iskon. hr...[color=blue]
    > Are they?
    > I mean, you could always downcast them for particular use, but then the
    > system would be well designed with intefaces instead of generics, and for
    > other uses I don't see any sense (collections excluded). Why do I need to
    > hold something of type 'object'? For what concrete usage? I am confused
    > with interfaces and generics... Please someone explain to me.
    >
    >[/color]


    Comment

    • luke

      #3
      Re: Are templates usefull?

      > With IEnumerator<T>, you don't have to cast, because the Current[color=blue]
      > property returns T. If you try to misassign this to another variable, you
      > catch it at compile time.
      >
      > Hope this helps.
      >[/color]

      Yes it does, thanks.


      Comment

      • Nick Hounsome

        #4
        Re: Are templates usefull?


        "luke" <luke@hotmail.c om> wrote in message
        news:dr8nlf$n27 $1@sunce.iskon. hr...[color=blue][color=green]
        >> With IEnumerator<T>, you don't have to cast, because the Current
        >> property returns T. If you try to misassign this to another variable,
        >> you catch it at compile time.
        >>
        >> Hope this helps.
        >>[/color]
        >
        > Yes it does, thanks.
        >[/color]

        But be aware that you can't just update all your old code because of at
        least 1 big gotcha [it got me :-(] IList<T> does NOT derive from IList (and
        there IS a good(ish) reason for this)

        This causes a problem where a method must continues to take IList and you
        are now trying to pass IList<T>


        Comment

        Working...