score:3
this won't work because there is no implicit conversion between list<doohickey>
and ienumerable<notifypropertychanging>
. you need to call:
update(doohickeys.cast<inotifypropertychanging>());
the reason behind this is type safety. in c#4, co-/contravariance was added to the language, which generally helps making these type of conversions valid. there are some limitations though, since the type involved needs to be declared co-/contravariant, and it only applies to interfaces and delegates.
the reason why you can't implicitly convert list<t>
to list<tbase>
is, that it would make this code valid:
list<t> values = new list<t>();
// add some values...
list<tbase> bases = values;
bases.add(new tbase()); // woops. we broke type safety by adding a tbase to a list of t's
now, if you tried to do this when values
was of type ienumerable<t>
, it would be valid in c#4. this is because you can only get values out of ienumerable<t>
, so we don't need to worry about lesser types being added. therefore ienumerable<t>
is covariant, and is declared as:
ienumerable<out t>
where out
means "covariant". (the keywords is actually the best way to remember which way values may go in a variant type.
co-/contravariance is a rather large subject, but this article does a good job of explaining the most important parts. and if you want to learn more, eric lippert has an 11-part blog post series about the feature. eric is one of the language designers.
Source: stackoverflow.com
Related Query
- Invalid Argument - Interfaces inherited, as works, implicit casting does not. What gives?
- The data source does not support server-side data paging
- Why does the Linq Cast<> helper not work with the implicit cast operator?
- System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>' could be found
- Entity Framework Many to Many works but Include does not
- Statement that works in .NET 3.5 throws "error CS0117: 'System.Linq.Dynamic.DynamicExpression' does not contain a definition for 'ParseLamba'" in 4.5
- CS1061: 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>'
- int' does not contain a definition for 'contains' and no extension method 'contains' accepting a first argument of type 'int' could be found
- Linq Boolean returns exception DROPDOWNLIST has a SelectedValue which is invalid because it does not exist in the list of item
- Code does not seem to pass a certain line, causing StackOverflowException
- Linq query with MongoDB works does not work as Func
- Task Does Not Contain a Definition for Where If Done in One Line of Code
- Linq - InvalidCastException - Why does "where" not filter the invalid types
- Convert code that uses Linq to code that does not
- Why Does IQueryable Select Has Invalid Arguments With DTO but works with object?
- List does not contain a definition for 'ConvertAll' and no extension method 'ConvertAll' accepting a first argument of type 'List< >'
- Code Conversion: EF.OrderBy as a string works in VB but not in C#
- EF GroupBy: does not contain a definition for 'BriefTitle' and no extension method 'BriefTitle' accepting a first argument of type
- MVC 5 ASP.NET IEnumerable<Item> does not contain a definition for "Inspection" and no extension method "inspection" accepting a first argument of type
- Why does my code not find directories
- The translation of String.IndexOf to SQL does not support versions with a StringComparison argument
- The query that works under Entity Framework 6 does not work in .Net5
- Why this multi linq join code does not work?
- From & To Date Filter on List Using LINQ working on Localhost but on IIS does not works
- 'LINQ.SalesTaxRate' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method
- LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'
More Query from same tag
- .NET Enumerable Union/Join combination
- Optimizing A List of athlete's competitions with LINQ query or similar approach
- Convert C# linq code to VB. [ Edited ]
- Why and When to use LINQ?
- Linq / Entity Framework not returning associated entities
- Order by one element and return multi properties in the same LINQ query
- EF Core 5 - In memory object list filtering not working against DBset
- Break from function composition on condition using LINQ
- Visual Studio 2015, missing library System.Xml.Linq
- Linq to Sql in ASP.Net MVC 4 returns actual query string
- Many-to-Many Select second order
- Office 365 Calendar API - Filtering Events by Date in C# Client Libraries
- Any() in a Chained Linq expression
- User selected date range to weeks via linq etc
- Trim string in LINQ C#
- Can not use group as class attribute name in Entity Framework select query
- Debugging LINQ on a per-element basis
- Generate a list holding/making mapping between a List<string> and a List<int>
- Does LINQ To SQL C# eliminate any possibility of a SQL injection attack?
- linq anon object utilizing parent object properties and child object properties
- How to get the list of adjacent items with the same name only
- LINQ for removing elements that are started with other element from list
- VS2005 and LINQ
- C# compilation error with LINQ and dynamic inheritance
- Why is generated query from expression using case instead of where statements?
- "where" query using linq xml
- Entity Framework Core 3.1 anonymous projections cannot now be used in Linq include statements?
- How do I search for a specific value in a generic List in C#?
- Parse string to C# lambda Func
- Simple Linq grouping