Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

generics and inheritance: List<T>

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jonpb - 28 Mar 2008 00:58 GMT
Hi,

The following code:

public class SubPoint2D : Point2D
{
  public int Code;
}

void ProcessPoints(List<Point2D> pts)
{
  return;
}

private void Test()
{
  List<SubPoint2D> subPts2D = new List<SubPoint2D>();

  ProcessPoints(subPts2D);
}

results in a compiler error: cannot convert List<SubPoint2D> to
List<Point2D>.

Is there a way to tell the compiler that SubPoint2D actually is a
Point2D and therefore the two List<T> types are equivalent as far as
Point2D? I tried:
ProcessPoints(subPts2D as List<Point2D>);
but that didn't work either.

Thanks,
John
Jon Skeet [C# MVP] - 28 Mar 2008 01:13 GMT
> The following code:
>
[quoted text clipped - 21 lines]
> Point2D and therefore the two List<T> types are equivalent as far as
> Point2D?

They're not equivalent at all.

Consider what happens when you try to add a Point2D to each of them:
with a List<Point2D> it's fine. With a List<SubPoint2D> it should go
bang.

Basically generics in C# don't support covariance. It's a pain, but it
also gives more safety. It's likely that C# 4 will have more support
for this, at the interface and delegate level, but that's all.

What you *can* do is make ProcessPoints a generic method:

void ProcessPoints(List<T> pts) where T : Point2D

That may help you, depending on what you need to do in ProcessPoints.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

jonpb - 28 Mar 2008 17:06 GMT
>> The following code:
>>
[quoted text clipped - 37 lines]
>
> That may help you, depending on what you need to do in ProcessPoints.

Thanks very much Jon. The generic method works in this particular case,
as all it does is draw the shape there is no manipulation of the lists
or changing of values, but your point is well taken.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.