473,402 Members | 2,053 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,402 software developers and data experts.

Bug in C# Compiler? (generics)

Hello,

Can anyone tell me why this won't compile. I'm getting the error:
"The type 'GenericsTest.GenericType3' must be convertible to
'GenericsTest.GenericType2<GenericsTest.IInterface 2>' in order to use it as
parameter 'T' in the generic type or method 'GenericsTest.BaseCollection<T>"

However, GenericType3 clearly inherits from GenericType2<IInterface2>

************ Code *************
#region Interface Hierarchy
public interface IInterface1
{}

public interface IInterface2 : IInterface1
{}

public class InterfaceType : IInterface2
{}
#endregion Interface Hierarchy

#region Generic Hierarchy
public abstract class GenericType1<T1, T2>
{}

public class GenericType2<T> : GenericType1<long, T>
where T : IInterface2
{}

public class GenericType3 : GenericType2<InterfaceType>
{ }
#endregion Generic Hierarcy
#region Collection Hierarchy
public abstract class BaseCollection<T>
where T : GenericType2<IInterface2>
{}

public class Collection : BaseCollection<GenericType3>
{ }
#endregion Collection Hierarchy
************ End Code *************

Thanks in advance for any help you can provide.

Mike
Apr 28 '06 #1
6 1357
AnotherMike <An*********@discussions.microsoft.com> wrote:
Can anyone tell me why this won't compile.
[...]
However, GenericType3 clearly inherits from GenericType2<IInterface2>
[...]


No it doesn't. It inherits from GenericType2<InterfaceType>, not
GenericType2<IInterface2>.

Given:

* a generic class G<T>
* a class C descended from
* a class B

So:

class G<T> { }
class B { }
class C : B { }

Now, there is no inheritance relationship between G<B> and G<C>. That is,
even though C is a subclass of B, G<C> is *not* a subclass of G<B>.

-- Barry
Apr 28 '06 #2
On Fri, 28 Apr 2006 09:17:01 -0700, AnotherMike <An*********@discussions.microsoft.com> wrote:
Hello,

Can anyone tell me why this won't compile. I'm getting the error:
"The type 'GenericsTest.GenericType3' must be convertible to
'GenericsTest.GenericType2<GenericsTest.IInterfac e2>' in order to use it as
parameter 'T' in the generic type or method 'GenericsTest.BaseCollection<T>"

However, GenericType3 clearly inherits from GenericType2<IInterface2>


What this code is infact trying to do is an assignment like this:

public class A
{
public void Foo()
{
B<String> item1 = new B<String>();
B<Object> item2 = item1;

item1.Add("abc");

//if the above assignment of item1 to item2 was allowed then according to item2
//this statement would be allowed, whilst it should not be allowed, as item1 must
//contain strings and anything that inherits from string.
item2.Add(new Object());
}
}

public class B<T>
{
public void Add(T item) { }
}
As shown an assignment of B<Object> item = new B<String> is invalid dispite the fact that string inherits from object.
In your code it was effectivly like trying to use an assignment reading:

GenericType2<IInterface2> item = new GenericType2<InterfaceType>

What you in fact want is to make the type GenericType2<IInterface2> in your BaseCollection generic, something like:

public class BaseCollection<T> where T : GenericType2<U> where U : IInterface2

unfortunatly this is not valid, and nether is:

public class BaseCollection<T<U>> where T : GenericType2<U> where U : IInterface2

This only leaves this:

public abstract class BaseCollection<T, U>
where T : GenericType2<U>
where U : IInterface2
{ }

but now using BaseCollection is messy:

public class Collection : BaseCollection<GenericType3, InterfaceType>
{ }
Note:
Is there a way to get nested generics to work?
As in, is their any way to express this:
public class BaseCollection<T<U>> where T : GenericType2<U> where U : IInterface2

or have the compiler infer that U exists in some way? By infer I mean in the same mannor it can work out that Push is
infact Push<int> without having to explictly tell it.

Stack<int> s = new Stack<int>;
Push(s, 1, 2 ,3) //didn't have to tell it that Push was <int> as s is Stack<int>

void Push<T>(Stack<T>, params T items);
Apr 28 '06 #3
Thank you Barry and Chris for your replies. You have been most helpful.
While you have answered my question about the possibility of what I was
trying to accomplish, it is still a mystery to me WHY this is the case. Why
can you not

GenericType2<IInterface2> item = new GenericType2<InterfaceType>;

when InterfaceType inherits from IInterface 2. I can understand why the
opposite would not work:

GenericType2<InterfaceType> item = new GenericType2<IInterface2>;

What is preventing the compiler from doing the conversion/cast?

Mike

"AnotherMike" wrote:
Hello,

Can anyone tell me why this won't compile. I'm getting the error:
"The type 'GenericsTest.GenericType3' must be convertible to
'GenericsTest.GenericType2<GenericsTest.IInterface 2>' in order to use it as
parameter 'T' in the generic type or method 'GenericsTest.BaseCollection<T>"

However, GenericType3 clearly inherits from GenericType2<IInterface2>

************ Code *************
#region Interface Hierarchy
public interface IInterface1
{}

public interface IInterface2 : IInterface1
{}

public class InterfaceType : IInterface2
{}
#endregion Interface Hierarchy

#region Generic Hierarchy
public abstract class GenericType1<T1, T2>
{}

public class GenericType2<T> : GenericType1<long, T>
where T : IInterface2
{}

public class GenericType3 : GenericType2<InterfaceType>
{ }
#endregion Generic Hierarcy
#region Collection Hierarchy
public abstract class BaseCollection<T>
where T : GenericType2<IInterface2>
{}

public class Collection : BaseCollection<GenericType3>
{ }
#endregion Collection Hierarchy
************ End Code *************

Thanks in advance for any help you can provide.

Mike

Apr 28 '06 #4
That behavior is called covariance...Interestingly, .NET supports it,
but C# doesn't.

John Skeet pointed to this link on the topic recently, which I found to
be pretty interesting:
http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx

A couple related discussions:
http://groups.google.com/group/micro...24741700287b0b
http://groups.google.com/group/micro...e9135d03ae9751

Good luck,
Jared

Apr 28 '06 #5
AnotherMike <An*********@discussions.microsoft.com> wrote:
Thank you Barry and Chris for your replies. You have been most helpful.
While you have answered my question about the possibility of what I was
trying to accomplish, it is still a mystery to me WHY this is the case. Why
can you not

GenericType2<IInterface2> item = new GenericType2<InterfaceType>;

when InterfaceType inherits from IInterface 2. I can understand why the
opposite would not work:

GenericType2<InterfaceType> item = new GenericType2<IInterface2>;

What is preventing the compiler from doing the conversion/cast?


C# doesn't support type parameter covariance/contravariance. The CLR
does, but the framework doesn't use it either.

The easiest example of why it doesn't work is using List. Consider the
following example, where Bar1 and Bar2 implement IFoo:

List<IFoo> list = new List<Bar1>();
list.Add (new Bar2());

See the problem? You end up trying to add a Bar2 reference to a list of
Bar1 references, even though Bar2 can't be converted to Bar1.

See http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx for
more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 28 '06 #6
But does anyone know of a simpler way to get the equivlent of this (which is invalid):

public class BaseCollection<T<U>>
where T : GenericType2<U>
where U : IInterface2

The best I could come up with was:

public abstract class BaseCollection<T, U>
where T : GenericType2<U>
where U : IInterface2

But this is now rather messy to work with and use.
Apr 29 '06 #7

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

Similar topics

4
by: 3rdshiftcoder | last post by:
Hi- I'd like to use this class from a book but am getting a compiler error. On the line private List < String > list; p.932 Chapter 19 found in the code below in the eclipse editor it is...
14
by: Mark Dufour | last post by:
After nine months of hard work, I am proud to introduce my baby to the world: an experimental Python-to-C++ compiler. It can convert many Python programs into optimized C++ code, without any user...
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...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
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...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
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...
12
by: jhc0033 | last post by:
There used to be an alpha quality JVM backend for RedHat's branch of GCC, but it was abandoned, because FOSS luminaries did not approve of Java (strange, because both are out to crush Microsoft) ...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.