473,387 Members | 3,787 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.

explicitly cast List<List<object>>

Hi,

If I have a class A, and a List<List<object>> containing instances of
A. How do I explicitly cast List<List<object>> as List<List<A>>?

Jun 17 '06 #1
4 12008
"colson" <ch******@gmail.com> a écrit dans le message de news:
11**********************@p79g2000cwp.googlegroups. com...

| If I have a class A, and a List<List<object>> containing instances of
| A. How do I explicitly cast List<List<object>> as List<List<A>>?

You can't. Do a Google search for covariance, this has been discussed many
times n these groups.

List<A> is not derived from List<object>, even though A is derived from
object; otherwise you could also add untyped objects to a List<A>, thus
defeating the type safety of generic classes.

BTW, why would you use a generic list to hold "object" instances, this
defeats the need for a generic list anyway, you might as well use an
ArrayList. Generic lists are for typesafe lists of instances of the same
type, not heterogeneous lists.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 17 '06 #2
colson <ch******@gmail.com> wrote:
If I have a class A, and a List<List<object>> containing instances of
A. How do I explicitly cast List<List<object>> as List<List<A>>?


You can't - they're not compatible. They wouldn't even be compatible if
you were dealing with single lists.

--
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
Jun 17 '06 #3
"colson" <ch******@gmail.com> wrote:
If I have a class A, and a List<List<object>> containing instances of
A. How do I explicitly cast List<List<object>> as List<List<A>>?


You cannot. Instantiated generic types have no inheritance relationship.
In particular, where type D descends from base class B, a type
parameterized with D is not a subtype of the same type parameterised
with B.

It doesn't make sense anyway. Consider:

---8<---
class B {}
class D1 : B {}
class D2 : B {}

// ...

List<D1> d = new List<D1>();

List<B> b = d;

b.Add(new D2());
--->8---

Notice how the call to "b.Add(new D2())" breaks type safety: an instance
of D2 was added to a list of D1, but D2 is *not* a D1! This would break
the whole point of the guarantees behind generics: improving development
experience by removing typecasting and improving runtime performance by
removing runtime checks.

-- Barry

--
http://barrkel.blogspot.com/
Jun 17 '06 #4
colson wrote:
Hi,

If I have a class A, and a List<List<object>> containing instances of
A. How do I explicitly cast List<List<object>> as List<List<A>>?


As already have been made clear by previous posters, you can't.

If you want to access an A object in the list, you have to cast that
specific instance to A:

A a = theList[outerIndex][innerIndex] as A;
if (a != null) {
...
}

Or if you are sure that the specific instance is of the correct class:

A a = (A)(theList[outerIndex][innerIndex]);
Jun 17 '06 #5

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

Similar topics

0
by: Pedro Werneck | last post by:
Hi list I'm trying to implement a new type in a C extension and it must support some binary operators, like &, |, ^, << and >>. With &, | and ^, the method must receive another object of the...
3
by: Henry Johnson | last post by:
Okay - I'm spinning my wheels on this one... can someone help me figure out how to programmatically populate a table cell as follows (from C# code-behind)? I've tried using a Literal control in the...
9
by: Mike Labosh | last post by:
Code is easy. These are two OOP design issues that I am chewing on. Coming from classic VB, I'm not great at OO design. I prefer to write mile long spaghetti methods in a Temp module, and then...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
3
by: jojowebdev | last post by:
Picking up from Friday.. I am still having trouble creating the Select Menu option. I got parts of the correct syntax but it is still not creating the selected carrier as an option in the parent...
4
by: Sin Jeong-hun | last post by:
List<List<T>a=param; List<List<T>b=a; If I change b, then a is get changed. I want another copy of a, that is completely independent of a. I used double-nested for loop to copy each element...
5
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
17
by: Cralis | last post by:
I am trying to populate a ListView with a list of 'Models' of cars. I have a data object class for my models, which has a function, 'getListOfModels', which I want to retuyrn a <Listof models. ...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
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: 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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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,...

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.