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

Generics confusion

Hi,

I've been playing with generics and I was wondering if anyone could
tell me why the following doesnt work, and if there is another way to
do it

public class A<Twhere T : B, new() {
public B Get() {
B b = new B();
b.X = this;
return b;
}
}

public class B {
public A<BX { set { } }
}

basicaly class A<Tknows that T is inherited from type B, but type B
contains a property X of type A<Band I'd like to be able to set this
property from class A<T>, which I would have though should work since
the compiler knows T is of type B, I cant even cast this to A<Bas a
workaround.

Any help appreciated

Jul 19 '07 #1
2 1272
public class A<Twhere T : B, new() {
public B Get() {
B b = new B();
b.X = this;
return b;
}
}

public class B {
public A<BX { set { } }
}

public class C : B
{
}

What happens when you create an instance of A<C>?

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
<jo**************@hotmail.comwrote in message
news:11*********************@57g2000hsv.googlegrou ps.com...
Hi,

I've been playing with generics and I was wondering if anyone could
tell me why the following doesnt work, and if there is another way to
do it

public class A<Twhere T : B, new() {
public B Get() {
B b = new B();
b.X = this;
return b;
}
}

public class B {
public A<BX { set { } }
}

basicaly class A<Tknows that T is inherited from type B, but type B
contains a property X of type A<Band I'd like to be able to set this
property from class A<T>, which I would have though should work since
the compiler knows T is of type B, I cant even cast this to A<Bas a
workaround.

Any help appreciated

Jul 19 '07 #2

<jo**************@hotmail.comwrote in message
news:11*********************@57g2000hsv.googlegrou ps.com...
Hi,

I've been playing with generics and I was wondering if anyone could
tell me why the following doesnt work, and if there is another way to
do it

public class A<Twhere T : B, new() {
public B Get() {
B b = new B();
b.X = this;
return b;
}
}

public class B {
public A<BX { set { } }
}

basicaly class A<Tknows that T is inherited from type B, but type B
contains a property X of type A<Band I'd like to be able to set this
property from class A<T>, which I would have though should work since
the compiler knows T is of type B, I cant even cast this to A<Bas a
workaround.
Your problem is that generics aren't covariant, so
T : B doesn't imply A<T: A<B>

For example:
Add a field Beta to A<Tof type T, and assume that B.X is implemented by a
backing field and has a getter as well. Now inside A<T>.Get(), you could
set b.X.Beta = new B(); Now this.Beta has dynamic type B, but is required
to have dynamic type T. Type-safety therefore requires that the compiler
reject the line "b.X = this".
Jul 19 '07 #3

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

Similar topics

2
by: Wiktor Zychla | last post by:
I've read several documents about upcoming C# generics and I still cannot understand one thing. Would it be valid to write a code like this: class SomeClass { public void AMethod<T>(T a, T...
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...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
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...
1
by: Peter | last post by:
Hi I'm trying to call a C# Method from with VB.NET and am having all sorts of troubles as I'm new to Generics. The C# method (which I didn't develop) is as follows: /// <summary> ///...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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.