473,387 Members | 1,834 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,387 software developers and data experts.

Compiler complains about type constraint in generic class

Hi there,

I have the following class hierarchy that isn't compiling correctly, and I
don't understand why:

public abstract class A<Twhere T : System.IComparable<T{ }
public abstract class B : System.IComparable<B>
{
public abstract int CompareTo(B other);
}
public abstract class C<T: A<Twhere T : B {}

The compiler (Visual Studio 2005) reports that class C has the following
error:

The type 'T' must be convertible to 'System.IComparable<T>' in order to use
it as parameter 'T' in the generic type or method 'A<T>'

I'm confused by this, because in class C, I'm constraining type T to be of
type class B, and class B is IComparable<B>. So, shouldn't class C's type T
therefore be IComparable<B>? Could someone explain why this is an error?

Thanks in advance!
--
Whitney Kew
Senior Software Engineer
Mar 30 '07 #1
1 4418

"Whitney Kew" <wm***@newsgroup.nospamwrote in message
news:BB**********************************@microsof t.com...
Hi there,

I have the following class hierarchy that isn't compiling correctly, and I
don't understand why:

public abstract class A<Twhere T : System.IComparable<T{ }
public abstract class B : System.IComparable<B>
{
public abstract int CompareTo(B other);
}
public abstract class C<T: A<Twhere T : B {}

The compiler (Visual Studio 2005) reports that class C has the following
error:

The type 'T' must be convertible to 'System.IComparable<T>' in order to
use
it as parameter 'T' in the generic type or method 'A<T>'

I'm confused by this, because in class C, I'm constraining type T to be of
type class B, and class B is IComparable<B>. So, shouldn't class C's type
T
therefore be IComparable<B>? Could someone explain why this is an error?

Thanks in advance!
Covariant interfaces aren't supported. And the constraint would be wrong
even if they were. Consider:

class D : B {}

D implements IComparable<B>, but not IComparable<D>. Therefore D cannot be
used as the parameter to A, so T : B is not sufficient to use A<T>.

Add a second type parameter to A:

public abstract class A<T, Uwhere T : System.IComparable<U>, T : U { }
public abstract class C<T: A<T, Bwhere T : B { }
--
Whitney Kew
Senior Software Engineer

Mar 30 '07 #2

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

Similar topics

3
by: Brad Wood | last post by:
I have this method declaration: private SomeType getSomething<T>( T spec ) where T: String, MemoryStream The compiler error I get is: 'string' is not a valid constraint. A type used as a...
2
by: Harold Howe | last post by:
Howdy all, I am getting a compiler error regarding a consrained conversion. It complains that it can't make the type conversion, even though the generic type argument inherits from the target of...
4
by: Ole Nielsby | last post by:
Here comes a generic class with a static property. Let's say they are exotic singleton pets. abstract class Pet {...} abstract class SharedPet<T>: Pet where T: SharedPet<T>, new() {...
6
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used...
4
by: Hyun-jik Bae | last post by:
Is that not allowed to assume generic type numeric type? As far as I've tried, I got an error with the following code: public class AAA<T> { public int Foo(T a) { return a; // error: Cannot...
3
by: Jeff Jarrell | last post by:
I want to provide a helper to apply a certificate to svcutil generated class(s) that derives from ClientBase<TChannel>. But the compiler can't find "TChannel". I have System.ServiceModel...
35
by: =?Utf-8?B?UElFQkFMRA==?= | last post by:
I'd really like to be able to constrain a generic type to System.Enum or, better, enum. But of course that results in "Compiler Error CS0702". So far I've been checking the type parameter at...
9
by: Andrus | last post by:
I need to create instance of generic type when generic type does not have parameterless constructor. I tried code below but got error shown in comment. How to fix ? Andrus. abstract class...
4
by: Weeble | last post by:
I have an interface that looks something like this: public interface IFoo<Twhere T : IFoo<T{ ... } Inside a class that looks something like this... public class Bar<V{ ... } ....I would like to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.