While I can raise a string to an object, I cannot convert IList strings to IList objects. How so? What to do now to others who cope with all subjects in the new IList?
static void ThisWorks() { IList<object> list = new List<object>(); list.Add("I can add a string since string : object"); } static void ThisDoesNotWork() { // throws an invalid cast exception IList<object> list = (IList<object>) new List<string>(); list.Add("I'm never getting here ... why?"); }
Look at it this way: while a banana is a fruit, a banana basket is not a fruit basket, as you can add oranges to the last, but not to the first. Yours List<string>has more severe limitations than List<object>.
List<string>
List<object>
Liskow. , , , , .
, generics ( # 3.0).
:
var objectList = list.Cast<object>().ToList();
, , , . , , . , .
string object, IList<string> IList<object>, , .
string
object
IList<string>
IList<object>
, , :
// THIS CODE DOES NOT WORK IList<object> list = new List<string>(); // this doesn't compile list.Add(5); // because this is perfectly valid on IList<object> but not on IList<string>
Source: https://habr.com/ru/post/1706670/More articles:Software developer with poor design skills - where to go? - designSubclass NSTextField - objective-cThe easiest way to support Tomcat with an Apache HTTP instance is tomcatIDE plug-in for developing multi-threaded network applications - javaWhy does Application_Start work in Cassini, but not in IIS7? - asp.netC #: creating an installer that installs a WPF application (ClickOnce) and a Windows service - c #How to write a schema function that takes two lists and returns four lists - lispAdvanced MySQL Am I missing a point? - mysqlWhy am I getting an exception raised from Spring.NET when I call ContextRegistry.GetContext ()? - c #Include "const void *" in "const char *" - castingAll Articles