If the Covariant parameters that are added in .Net Framework 4 allow me to make assignments that look like polymorphism
Why can I write
IEnumerable<object> lst = new List<string>();
And I can not write
List<object> lst = new List<string>();
There are two reasons why this does not work:
IEnumerable<T>is covariant (declared public interface IEnumerable<out T>), but IList<T>not. Otherwise, you can do something like this:
IEnumerable<T>
public interface IEnumerable<out T>
IList<T>
IList<string> listOfStrings = ... IList<object> listOfObjects = listOfStrings; listOfObjects.Add(42); // would fail at runtime because it is actually a list of strings, not ints
Covariance and contravariance only work on interfaces, not specific classes.
From MSDN :
.NET Framework 4 .
List {T} , :
List<object> lst = new List<string>(); lst.Add(10);
, Int32 .
IEnumerable {out T} , 'out'. , , , .
, , :
List<object> lst = new List<object>(); lst.AddRange(new string[] { "hello" });
Source: https://habr.com/ru/post/1761626/More articles:Buil Android Lite Version - androidCan the module upload files? - phpSharePoint 2010 / IIS 7.5. Request byte range Responds to the whole file - http-headersConfiguring matplotlib image display to add copy / paste - pythonSSIS - creating a custom connection manager - sql-serverthat empty regular expressions match in ruby? - ruby | fooobar.comKeypress Detection - cSelenium with Python, how do I get page output after running a script? - pythoncapture java mouse - javaMultilingual support in my javascript code - javascriptAll Articles