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

Easy Generics Question

Let's say I have an interface IScope:

internal interface IScope{}

....and I have a class that implements IScope:

internal class Scope:IScope{}

.... and I have a method that returns List<Scope>:

public List<ScopeGetScopes(){return new List<Scope>();}

.... and I'd like to pass the return value from the above method to another
method that takes a List<IScope>, say a method that looks like:

public void ProcessScopes(List<IScopescopes){}

.... I figured that a List<Scopecould be directly converted to List<IScope>
since Scope derives from IScope, but apparently I was wrong. The conversion
doesn't work, even with an explicit cast. Could someone explain the
rationale for *why* it doesn't work? My first attempt at a workaround for
this was to build the following conversion method -- something like:

public static List<UUpcastList<U,D>(List<DlstD) where D:U{
List<UlstU=new List<U>();
foreach(D d in lstD){
lstU.Add(d);
}
return lstU;
}

..this seems to work for me, but I'm still perplexed as to why the
direct conversion above fails at compile time. Any comments would be
appreciated.

Thanks..
Sep 20 '06 #1
4 1214
Drama,

Let's assume that this does work. So you have this:

List<Scope>

And in the class definition of List<Tyou have something like this
(just assuming, it doesn't matter really):

List<T>
{
private T[] items;
}

So when you use List<Scope>, items is declared as type Scope[].

Now, say you have declared a class, Scope2, which implements IScope, but
does NOT derive from Scope, like so:

class Scope2 : IScope
{}

Assuming you allowed a cast from List<Scopeto List<IScope>:

// This is not legal and will not compile.
List<IScopeiScopeList = (List<IScope>) scopeList;

And then you make the call:

iScopeList.Add(new Scope2());

It would have to fail. The reason being that while you could take a
parameter of IScope, the type itself is configured to use Scope instances.
You can't just change that. Because Scope2 doesn't derive from Scope, you
can't handle the case where fields of the parameterized type are held
internally.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Johnny Drama" <gh****@englewood.comwrote in message
news:kb******************************@giganews.com ...
Let's say I have an interface IScope:

internal interface IScope{}

...and I have a class that implements IScope:

internal class Scope:IScope{}

... and I have a method that returns List<Scope>:

public List<ScopeGetScopes(){return new List<Scope>();}

... and I'd like to pass the return value from the above method to another
method that takes a List<IScope>, say a method that looks like:

public void ProcessScopes(List<IScopescopes){}

... I figured that a List<Scopecould be directly converted to
List<IScopesince Scope derives from IScope, but apparently I was wrong.
The conversion doesn't work, even with an explicit cast. Could someone
explain the rationale for *why* it doesn't work? My first attempt at a
workaround for this was to build the following conversion method --
something like:

public static List<UUpcastList<U,D>(List<DlstD) where D:U{
List<UlstU=new List<U>();
foreach(D d in lstD){
lstU.Add(d);
}
return lstU;
}

..this seems to work for me, but I'm still perplexed as to why the
direct conversion above fails at compile time. Any comments would be
appreciated.

Thanks..

Sep 20 '06 #2
Hi Johnny,

Technically speaking, each List<Tdeclared in your code, where T differs, is created as a distinct class behind the scenes.
Although Scope implements IScope, List<Scopedoesn't derive from List<IScope>. Each are distinct classes that do not share the
same inheritance chain beyond IList and its derived interfaces. i.e., It's like casting an apple into an orange, so to speak. The
fact that the generic Types share the same inheritance chain is irrelevant.

This might be confusing since Scope[] may be passed to a method that expects IScope[], but that is because Array semantics are
handled by the runtime. The generic List class provides array semantics on a higher-level then Array and the runtime is unaware of
such an implementation and so treats it as it would any other class in the framework.

--
Dave Sexton

"Johnny Drama" <gh****@englewood.comwrote in message news:kb******************************@giganews.com ...
Let's say I have an interface IScope:

internal interface IScope{}

...and I have a class that implements IScope:

internal class Scope:IScope{}

... and I have a method that returns List<Scope>:

public List<ScopeGetScopes(){return new List<Scope>();}

... and I'd like to pass the return value from the above method to another method that takes a List<IScope>, say a method that
looks like:

public void ProcessScopes(List<IScopescopes){}

... I figured that a List<Scopecould be directly converted to List<IScopesince Scope derives from IScope, but apparently I was
wrong. The conversion doesn't work, even with an explicit cast. Could someone explain the rationale for *why* it doesn't work? My
first attempt at a workaround for this was to build the following conversion method -- something like:

public static List<UUpcastList<U,D>(List<DlstD) where D:U{
List<UlstU=new List<U>();
foreach(D d in lstD){
lstU.Add(d);
}
return lstU;
}

..this seems to work for me, but I'm still perplexed as to why the direct conversion above fails at compile time. Any comments
would be appreciated.

Thanks..

Sep 20 '06 #3
In microsoft.public.dotnet.languages.csharp Johnny Drama <gh****@englewood.comwrote:
Let's say I have an interface IScope:

internal interface IScope{}

...and I have a class that implements IScope:

internal class Scope:IScope{}

... and I have a method that returns List<Scope>:

public List<ScopeGetScopes(){return new List<Scope>();}
List<ScopeoldScopes = new List<Scope>();
oldScopes.Add(new Scope());
List<IScopescopes = new List<IScope>(oldScopes);

I hope that helps you out.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Sep 20 '06 #4
Thanks, guys!! I now understand.

much appreciated,

Johnny
Sep 20 '06 #5

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

Similar topics

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...
6
by: Jules Winfield | last post by:
I'm loving VS 2005. These are my questions and comments so far: BUG: Autoformatting ================ I've turned off virtually all autoformatting, but there is still one place where it rears...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.