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

cannot convert

Hi,

I have a method in a class:

public void AddUserList(List<IUserusers)
{
...
}

which adds a list of users to the class.

I also have a user class:

public class User : IUser
{
...
}

Why is it I cannot perform a call like this:

List<Userusers = new List<User>();
AddUserList(users);
I would have naively thought that as User implements IUser, I could pass a
list of User objects to a method which expects a list of IUser objects (as
a User is an IUser).
Thanks,
Peter
Sep 6 '07 #1
4 1434
On Sep 6, 8:43 am, Peter K <xdz...@hotmail.comwrote:

<snip>
Why is it I cannot perform a call like this:

List<Userusers = new List<User>();
AddUserList(users);

I would have naively thought that as User implements IUser, I could pass a
list of User objects to a method which expects a list of IUser objects (as
a User is an IUser).
Nope. Suppose the AddUserList implementation was this:

users.Add (new OtherIUserImplementation());

At that point your List<Userwould contain a reference to something
which isn't a User.

Basically, generics (at least as exposed in C#) don't support
covariance/contravariance. This gives more compile-time safety at the
cost of some convenience.

See http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx for
more information.

Jon

Sep 6 '07 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in
news:11*********************@57g2000hsv.googlegrou ps.com:
On Sep 6, 8:43 am, Peter K <xdz...@hotmail.comwrote:

<snip>
>Why is it I cannot perform a call like this:

List<Userusers = new List<User>();
AddUserList(users);

I would have naively thought that as User implements IUser, I could
pass a list of User objects to a method which expects a list of IUser
objects (as a User is an IUser).

Nope. Suppose the AddUserList implementation was this:

users.Add (new OtherIUserImplementation());

At that point your List<Userwould contain a reference to something
which isn't a User.
I'm not following you here... If I have a list defined
List<Userusers
then I obviously can't add an OtherUser object, because the list is only
for User objects.

But If I had this list:
List<IUserusers

I would have thought I could add a User:IUser and an OtherUser:IUser, as
the list is defined as accepting IUser objects, and both my objects are
IUser objects.
But my question was about supplying the list to a method, not about
adding objects to the list (maybe it's the same question?).

Basically, generics (at least as exposed in C#) don't support
covariance/contravariance. This gives more compile-time safety at the
cost of some convenience.

See http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx for
more information.
I'll have a look.

Thanks,
Peter
Sep 6 '07 #3
Peter K wrote:
Hi,

I have a method in a class:

public void AddUserList(List<IUserusers)
{
...
}

which adds a list of users to the class.

I also have a user class:

public class User : IUser
{
...
}

Why is it I cannot perform a call like this:

List<Userusers = new List<User>();
AddUserList(users);
I would have naively thought that as User implements IUser, I could pass a
list of User objects to a method which expects a list of IUser objects (as
a User is an IUser).
Yep, that is a naive thought. If the method want List<IUser>, you have
to give List<IUser>. So, either make

public void AddUserList(List<Userusers)
...
List<User>users = new List<User>()

or

public void AddUserList(List<IUserusers)
...

List<IUser>users = new List<IUser>()

(Actually, I'd use IList, but that is another story).

--
Arto Viitanen
>
Thanks,
Peter
Sep 6 '07 #4
On Sep 6, 9:10 am, Peter K <xdz...@hotmail.comwrote:
Nope. Suppose the AddUserList implementation was this:
users.Add (new OtherIUserImplementation());
At that point your List<Userwould contain a reference to something
which isn't a User.

I'm not following you here... If I have a list defined
List<Userusers
then I obviously can't add an OtherUser object, because the list is only
for User objects.
Indeed - but AddUserList doesn't know it's a List<User>. It only knows
that it has a List<IUser>, which is the point. There's nothing (at
compile time) to stop AddUserList being implemented with the line I
showed.

<snip>
But my question was about supplying the list to a method, not about
adding objects to the list (maybe it's the same question?).
Indeed it is - you've got to think about what AddUserList *might*
(legally) do given its signature - that explains why you can't pass in
something that would break if it did that.

Jon

Sep 6 '07 #5

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

Similar topics

1
by: Michel | last post by:
Hi again all, I have a small issue. Here's an example dataset : F1 F2 F3 1 0.58 Hi 2 0.70 Hello 3 Fail Bye 4 <Null> Hi
22
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = ((...
3
by: Laura T. | last post by:
The following code is driving me so crazy, that I'm thinking it's a "feature" or a.. ....dare I say it... a BUG. I'm using VS 2003. I've created a new value type (struct) called MyInt. MyInt has...
3
by: Chris | last post by:
Hi, I have the following procedure below: I am getting 2 errors on compile: Argument '2': cannot convert from 'System.Data.SqlClient.SqlDataReader' to 'ref...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
2
by: Christophe | last post by:
class A {} class B {} interface MyInterface { void method(A a); void method(B b); }
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
6
by: John | last post by:
The following code: int test = 1; bool isTrue = (bool)test; results in a compiler error: Cannot convert type 'int' to 'bool' wtf, any ideas on how to work around this?
1
by: gdavid | last post by:
Hi. I Have started using Visual C++ 2008 a month ago. I Tried to make a GUI calculator but i cannot convert the textbox's text with the atof() function. The function cannot convert String^ type into...
1
by: Cezus | last post by:
Hello, I cannot convert the following query in the dataset to a string. It says it cannot get more then 2034 chars long... the string just ends at 2034 characters... this is where it goes...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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,...

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.