473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Nov 16 '05 #1
5 1301
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 Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Mattias Sjögren wrote:
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

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
Nov 16 '05 #3
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 Sj?gren wrote:
Jeremy,

but can anyone
suggest a solution for this?

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]

Nov 16 '05 #4
Jeremy Gailor <je****@infinitecube.com> wrote:
Use a constraint to ensure that the type argument implements
IComparable<T>, and use that for comparison instead.

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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Jeremy Gailor <je****@infinitecube.com> wrote:
Use a constraint to ensure that the type argument implements
IComparable<T>, and use that for comparison instead.

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


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fred | last post by:
Is there a way to use generics with vs.net 2003?? I was searching around and I saw gyro but does it works with vs.net 2003?? if it does how am I suppouse to install it. If it doesnt is there...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
13
by: Luc Vaillant | last post by:
I try to compare to values of generic value type T in a generic class as follow: public class C<T> where T : struct { private T value1; private T value2; C(T value1, T value2) {
1
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
1
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.