C# generics question

  • Thread starter Thread starter Jeremy Gailor
  • Start date Start date
J

Jeremy Gailor

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
 
Jeremy,
but can anyone
suggest a solution for this?

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



Mattias
 
Mattias said:
Jeremy,




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



Mattias


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
 
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
http://staff.develop.com/richardb/weblog

?
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Mattias said:
Jeremy,




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



Mattias


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.public.dotnet.languages.csharp]
 
Jeremy Gailor said:
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.

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 said:
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>?

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.)
 
Back
Top