Click here to Skip to main content
15,914,111 members
Home / Discussions / C#
   

C#

 
QuestionHow to write an element address of array in C# Pin
pvpeng28-Jun-14 16:58
pvpeng28-Jun-14 16:58 
AnswerRe: How to write an element address of array in C# Pin
Pete O'Hanlon28-Jun-14 19:42
mvePete O'Hanlon28-Jun-14 19:42 
AnswerRe: How to write an element address of array in C# Pin
Kornfeld Eliyahu Peter28-Jun-14 19:57
professionalKornfeld Eliyahu Peter28-Jun-14 19:57 
QuestionMessage Closed Pin
28-Jun-14 9:35
Csíkszentmihályi28-Jun-14 9:35 
QuestionCan the recall frmLogIn ? Pin
Member 245846728-Jun-14 5:38
Member 245846728-Jun-14 5:38 
AnswerRe: Can the recall frmLogIn ? Pin
Richard Deeming30-Jun-14 1:51
mveRichard Deeming30-Jun-14 1:51 
GeneralRe: Can the recall frmLogIn ? Pin
Member 24584674-Jul-14 21:02
Member 24584674-Jul-14 21:02 
GeneralRe: Can the recall frmLogIn ? Pin
Richard Deeming7-Jul-14 2:02
mveRichard Deeming7-Jul-14 2:02 
GeneralRe: Can the recall frmLogIn ? Pin
Member 24584678-Jul-14 20:49
Member 24584678-Jul-14 20:49 
QuestionTrying to get List<T> through reflection Pin
joost.versteegen28-Jun-14 5:29
joost.versteegen28-Jun-14 5:29 
AnswerRe: Trying to get List<T> through reflection Pin
Alan N28-Jun-14 6:12
Alan N28-Jun-14 6:12 
GeneralRe: Trying to get List<T> through reflection Pin
joost.versteegen28-Jun-14 23:20
joost.versteegen28-Jun-14 23:20 
AnswerRe: Trying to get List<T> through reflection Pin
Alan N29-Jun-14 0:55
Alan N29-Jun-14 0:55 
GeneralRe: Trying to get List<T> through reflection Pin
joost.versteegen29-Jun-14 1:12
joost.versteegen29-Jun-14 1:12 
AnswerRe: Trying to get List<T> through reflection Pin
PIEBALDconsult28-Jun-14 7:48
mvePIEBALDconsult28-Jun-14 7:48 
AnswerRe: Trying to get List<T> through reflection Pin
Richard Deeming30-Jun-14 1:47
mveRichard Deeming30-Jun-14 1:47 
Even without using reflection, you won't be able to cast a List<Person> to a List<object>, because that would let you break the list.

Consider what would happen if this cast was allowed:
C#
List<Person> onlyContainsPeople = new List<Person>();
List<object> containsAnyObject = (List<object>)onlyContainsPeople;
containsAnyObject.Add(new Hippopotamus()); // <-- Not a Person!
Person thePerson = onlyContainsnPeople[0];

You now have a list which is guaranteed to only contain Person objects, but the only object in the list is not a Person object. Either the line which adds the item to the list would have to throw an exception, or the line which retrieves the item from the list would have to throw an exception. Neither exception would be obvious or expected.


There are a few generic interfaces and delegates which, in .NET 4.0 or higher, are declared as covariant or contravariant, which will allow something similar to what you're trying to do:
C#
List<Person> onlyContainsPeople = new List<Person>();
IEnumerable<object> containsAnyObject = onlyContainsPeople;

Generic covariance only works for read-only interfaces/delegates - the generic type parameter is only ever returned, never passed in. Contravariance only works when the type parameter is only ever passed in, never returned.

See Covariance and Contravariance in Generics[^] on MSDN for more information.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


QuestionThesis Project Pin
jomilyn28-Jun-14 3:02
jomilyn28-Jun-14 3:02 
AnswerRe: Thesis Project Pin
Pete O'Hanlon28-Jun-14 3:07
mvePete O'Hanlon28-Jun-14 3:07 
GeneralRe: Thesis Project Pin
jomilyn28-Jun-14 3:33
jomilyn28-Jun-14 3:33 
GeneralRe: Thesis Project Pin
OriginalGriff28-Jun-14 3:59
mveOriginalGriff28-Jun-14 3:59 
GeneralRe: Thesis Project Pin
jomilyn28-Jun-14 4:07
jomilyn28-Jun-14 4:07 
GeneralRe: Thesis Project Pin
OriginalGriff28-Jun-14 4:11
mveOriginalGriff28-Jun-14 4:11 
GeneralRe: Thesis Project Pin
Tom Delany30-Jun-14 12:06
Tom Delany30-Jun-14 12:06 
Questioncannot found emailhandler in c# 2008 Pin
KaKoten27-Jun-14 22:19
KaKoten27-Jun-14 22:19 
AnswerRe: cannot found emailhandler in c# 2008 Pin
OriginalGriff27-Jun-14 22:34
mveOriginalGriff27-Jun-14 22:34 

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.