I tried to make a conversion from an object (the object contains List<Apple>) to List<object>, and it failed with an error:
Cannot pass an object of type "System.Collections.Generic.List [list_cast_object_test.Apple]" to enter "System.Collections.Generic.List [System.Object]".
When I replaced Listwith IEnumerableor IList(interface), it works well, and I do not understand the difference ....
The code looks like this:
private void Form1_Load(object sender, EventArgs e) {
object resultObject = GetListAsObject();
var resAsIt = (List<Apple>)resultObject;
var resAsIEnumerable = (IEnumerable<object>)resultObject;
var resAsList = (List<object>)resultObject;
}
private object GetListAsObject() {
List<Apple> mere = new List<Apple>();
mere.Add(new Apple { Denumire = "ionatan", Culoare = "rosu" });
mere.Add(new Apple { Denumire = "idared", Culoare = "verde" });
return (object)mere;
}
}
public class Apple {
public string Denumire { get; set; }
public string Culoare { get; set; }
}
Can anyone explain to me what this is? What is the difference between casting to a common interface and casting to a generic class?