Click here to Skip to main content
15,897,187 members
Home / Discussions / C#
   

C#

 
GeneralRe: Simple problem with brackets Pin
Amangang4-Aug-09 22:59
Amangang4-Aug-09 22:59 
GeneralRe: Simple problem with brackets Pin
Amangang5-Aug-09 2:15
Amangang5-Aug-09 2:15 
GeneralRe: Simple problem with brackets Pin
VengefulSakhmet5-Aug-09 3:34
VengefulSakhmet5-Aug-09 3:34 
GeneralRe: Simple problem with brackets Pin
Amangang5-Aug-09 3:41
Amangang5-Aug-09 3:41 
QuestionCasting List<concreteclass> to IList<interfaceconcreclass></interfaceconcreclass></concreteclass> Pin
Ubirajara Mendes4-Aug-09 8:49
Ubirajara Mendes4-Aug-09 8:49 
AnswerRe: Casting List to IList Pin
Adam Maras4-Aug-09 9:31
Adam Maras4-Aug-09 9:31 
GeneralRe: Casting List to IList Pin
Ubirajara Mendes5-Aug-09 9:03
Ubirajara Mendes5-Aug-09 9:03 
AnswerRe: Casting List to IList Pin
Adam Maras5-Aug-09 11:13
Adam Maras5-Aug-09 11:13 
Ahh... I see the problem now.

Your problem is one related to covariance and contravariance[^]. Let me show you an example:

public interface IMyClass { }

public class MyClass : IMyClass { }
public class MyOtherClass : IMyClass { }
In this example, you have an interface, and two classes that implement said interface. Now, you've created a generic list of your first class type:
List<MyClass> listMyClass = new List<MyClass>();
Let's say, for the sake of this argument you weren't getting that cast error, and you could cast to a generic list of your interface type:
List<IMyClass> listIMyClass = ( List<IMyClass> )listMyClass;

Now, while only one list exists (because your cast just changed the type of the reference, not the contents of the list) you have created a reference that would allow you to add other objects that implement IMyClass to that list. Think about this:
listMyClass.Add( new MyOtherObject() );  // obviously won't work, MyObject (the list type)
                                         // and MyOtherObject (the type being added) are incompatible

listIMyClass.Add( new MyOtherObject() ); // seems like it should work, because you're adding an object
                                         // that implements IMyClass... but that list you're referencing
                                         // has a generic type of MyClass, NOT IMyClass.

The problem isn't that you're casting a List<> to an IList<>, it's that you're trying to cast from a list with one generic parameter type to another.

Adam Maras | Software Developer
Microsoft Certified Professional Developer

GeneralRe: Casting List to IList Pin
Ubirajara Mendes6-Aug-09 8:50
Ubirajara Mendes6-Aug-09 8:50 
Questiondrag n drop images Pin
strife194-Aug-09 7:39
strife194-Aug-09 7:39 
AnswerRe: drag n drop images Pin
Abhijit Jana4-Aug-09 8:08
professionalAbhijit Jana4-Aug-09 8:08 
QuestionFacotry Design Pattern Pin
jpk4204-Aug-09 6:49
jpk4204-Aug-09 6:49 
AnswerRe: Facotry Design Pattern Pin
Abhijit Jana4-Aug-09 6:56
professionalAbhijit Jana4-Aug-09 6:56 
AnswerRe: Facotry Design Pattern Pin
N a v a n e e t h4-Aug-09 7:05
N a v a n e e t h4-Aug-09 7:05 
QuestionUse of Interfaces Pin
jpk4204-Aug-09 6:47
jpk4204-Aug-09 6:47 
AnswerRe: Use of Interfaces Pin
Abhijit Jana4-Aug-09 7:05
professionalAbhijit Jana4-Aug-09 7:05 
AnswerRe: Use of Interfaces Pin
N a v a n e e t h4-Aug-09 7:18
N a v a n e e t h4-Aug-09 7:18 
AnswerRe: Use of Interfaces Pin
Luc Pattyn4-Aug-09 7:42
sitebuilderLuc Pattyn4-Aug-09 7:42 
Questionquery wizard Pin
mohammad alnoed4-Aug-09 6:46
mohammad alnoed4-Aug-09 6:46 
AnswerRe: query wizard Pin
Abhijit Jana4-Aug-09 7:00
professionalAbhijit Jana4-Aug-09 7:00 
GeneralRe: query wizard Pin
mohammad alnoed4-Aug-09 7:07
mohammad alnoed4-Aug-09 7:07 
GeneralRe: query wizard Pin
Henry Minute4-Aug-09 13:19
Henry Minute4-Aug-09 13:19 
QuestionAsynchronous socket and webrequest Pin
joana.simoes4-Aug-09 6:36
joana.simoes4-Aug-09 6:36 
QuestionFile In Use Problem Pin
eddieangel4-Aug-09 6:03
eddieangel4-Aug-09 6:03 
AnswerRe: File In Use Problem [modified] Pin
Luc Pattyn4-Aug-09 6:08
sitebuilderLuc Pattyn4-Aug-09 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.