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

Generic collection casting

Hi,

I would like to have something like that :

IList<IUser> list2 = list1 as IList<IUser>;

where list1 is a generic collection of User ( IList list1<User>=new
List<User>() ) and where User implement the interface IUser.
But this did not work...
I thought that as User implement IUser it was possible to cast the generic
collection between the two types. Currently I ve to use a foreach loop to
populate the list2 collection :
foreach(User u in list1)
{
list2.add(u as IUser);
}

Is there a more efficient and elegant way for doing this ?

Thanks.
Yalt.


Apr 14 '06 #1
2 1705
The problem here is that while the User implements the IUser interface, and
therefore an instance of User can be cast as IUser, IList<IUser> is a *List*
of IUser, and IList<User> is a *List* of User. You're not trying to cast the
items *u=in* the list; you're trying to cast the List itself. List<User>
does not inherit List<IUser> and List<IUser> is not an interface which
List<User> Implements. Therefore, there is no casting between them.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"cylt" <cy**@newsgroups.nospam> wrote in message
news:E9**********************************@microsof t.com...
Hi,

I would like to have something like that :

IList<IUser> list2 = list1 as IList<IUser>;

where list1 is a generic collection of User ( IList list1<User>=new
List<User>() ) and where User implement the interface IUser.
But this did not work...
I thought that as User implement IUser it was possible to cast the generic
collection between the two types. Currently I ve to use a foreach loop to
populate the list2 collection :
foreach(User u in list1)
{
list2.add(u as IUser);
}

Is there a more efficient and elegant way for doing this ?

Thanks.
Yalt.

Apr 14 '06 #2
Thank you for your response,

I understand now what was wrong.

Yalt.
"Kevin Spencer" wrote:
The problem here is that while the User implements the IUser interface, and
therefore an instance of User can be cast as IUser, IList<IUser> is a *List*
of IUser, and IList<User> is a *List* of User. You're not trying to cast the
items *u=in* the list; you're trying to cast the List itself. List<User>
does not inherit List<IUser> and List<IUser> is not an interface which
List<User> Implements. Therefore, there is no casting between them.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"cylt" <cy**@newsgroups.nospam> wrote in message
news:E9**********************************@microsof t.com...
Hi,

I would like to have something like that :

IList<IUser> list2 = list1 as IList<IUser>;

where list1 is a generic collection of User ( IList list1<User>=new
List<User>() ) and where User implement the interface IUser.
But this did not work...
I thought that as User implement IUser it was possible to cast the generic
collection between the two types. Currently I ve to use a foreach loop to
populate the list2 collection :
foreach(User u in list1)
{
list2.add(u as IUser);
}

Is there a more efficient and elegant way for doing this ?

Thanks.
Yalt.


Apr 14 '06 #3

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

Similar topics

1
by: conchur | last post by:
Is there an elegant way to have a class utilise an inherited method to return a Typed Collection of itself? Example: class MyList<T> : List<T> where T : MyRoot { Add... Remove... }
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
4
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have written a generic method which does different things depending on the type of the parameter. I got it to work, but it seems really inelegant. Is there a better way to do this? In the...
3
by: daokfella | last post by:
I simply want to extend the List<Tobject to include a property called VirtualCount. Basically, the list will hold my paged records, but the VirtualCount property will tell me how many records total...
8
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.