C# generics question

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

    C# generics question

    I'm currently just playing around with some AI stuff (working with
    breadth-first, depth-first searches (irrelevant)), but I'm writing a
    binary tree using the generics just to get a feel for them. I can't
    however implement the tree because I can't use the '<' or '>' to compare
    values for insertion into the tree. I don't know if there is a
    BinaryTree already provided as a generic in the .Net framework (seems
    like it would violate the rules I'm working with now), but can anyone
    suggest a solution for this? I would prefer to be able to leave it as a
    generic, rather than using 'object' 's to avoid boxing/unboxing
    operations on value types.

    Thanks in advance
  • Mattias Sjögren

    #2
    Re: C# generics question

    Jeremy,
    [color=blue]
    >but can anyone
    >suggest a solution for this?[/color]

    Use a constraint to ensure that the type argument implements
    IComparable<T>, and use that for comparison instead.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Jeremy Gailor

      #3
      Re: C# generics question

      Mattias Sjögren wrote:
      [color=blue]
      > Jeremy,
      >
      >[color=green]
      >>but can anyone
      >>suggest a solution for this?[/color]
      >
      >
      > Use a constraint to ensure that the type argument implements
      > IComparable<T>, and use that for comparison instead.
      >
      >
      >
      > Mattias
      >[/color]


      I've tried your suggestion, but it still looks there is a box operation
      occuring in the CIL, which is what I'm trying to avoid by using the
      generics over using 'object' as my parameter types.

      Any suggestions on how to make this work/other ways around this problem?

      Thanks in advance

      Comment

      • Richard Blewett [DevelopMentor]

        #4
        Re: C# generics question

        One of the reasons to introduce generics was to *reduce* the amount of boxing - but another was to allow the writing of generic code. In general boxing is not a huge problem unless you are executing something which boxes in a tight loop or have implemented a mutator interface on a value type (in which case you may update something other than the thing you *think* you are updating).

        IComparable is the interface that is used for the concept of *difference* between instances of types - yes if you use a value type in this generic you will see a box, but in your situation is this that significant in real terms (object creation is incredibly cheap in .NET). Is it enough of an issue to go outside of the standard mechanism of indicating a type supports the concept of ordering.

        Regards

        Richard Blewett - DevelopMentor


        ?
        nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/
        Mattias Sj?gren wrote:
        [color=blue]
        > Jeremy,
        >
        >[color=green]
        >>but can anyone
        >>suggest a solution for this?[/color]
        >
        >
        > Use a constraint to ensure that the type argument implements
        > IComparable, and use that for comparison instead.
        >
        >
        >
        > Mattias
        >[/color]


        I've tried your suggestion, but it still looks there is a box operation occuring in the CIL, which is what I'm trying to avoid by using the generics over using 'object' as my parameter types.

        Any suggestions on how to make this work/other ways around this problem?

        Thanks in advance

        [microsoft.publi c.dotnet.langua ges.csharp]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: C# generics question

          Jeremy Gailor <jeremy@infinit ecube.com> wrote:[color=blue][color=green]
          > > Use a constraint to ensure that the type argument implements
          > > IComparable<T>, and use that for comparison instead.
          > >[/color]
          > I've tried your suggestion, but it still looks there is a box operation
          > occuring in the CIL, which is what I'm trying to avoid by using the
          > generics over using 'object' as my parameter types.[/color]

          Are you sure you're using IComparable<T> instead of IComparable? With
          IComparable<T> I don't *think* there should be any boxing going on.
          Alternatively, have you tried IComparer<T>?

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: C# generics question

            Jon Skeet [C# MVP] <skeet@pobox.co m> wrote:[color=blue]
            > Jeremy Gailor <jeremy@infinit ecube.com> wrote:[color=green][color=darkred]
            > > > Use a constraint to ensure that the type argument implements
            > > > IComparable<T>, and use that for comparison instead.
            > > >[/color]
            > > I've tried your suggestion, but it still looks there is a box operation
            > > occuring in the CIL, which is what I'm trying to avoid by using the
            > > generics over using 'object' as my parameter types.[/color]
            >
            > Are you sure you're using IComparable<T> instead of IComparable? With
            > IComparable<T> I don't *think* there should be any boxing going on.
            > Alternatively, have you tried IComparer<T>?[/color]

            Ah, thinking about it, the boxing may be due to invoking an interface
            implementation on a value type.

            Using IComparer<T> should definitely help. (Put the implementation in a
            class rather than a struct.)

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...