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 Articles
- Invalid Argument - Interfaces inherited, as works, implicit casting does not. What gives?
- Why Does IQueryable Select Has Invalid Arguments With DTO but works with object?
- Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
- The data source does not support server-side data paging
- What does this C# code with an "arrow" mean and how is it called?
- Why does a Linq Cast<T> operation fail when I have an implicit cast defined?
- Why does the Linq Cast<> helper not work with the implicit cast operator?
- Does this LINQ code perform multiple lookups on the original data?
- System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of
- Does this code really cause an "access to modified closure" problem?
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- How does this linq code that splits a sequence work?
- Does MongoDb C# driver works with LINQ and dynamic documents?
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- .NET 4 Code Contracts: "requires unproven: source != null"
- '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
- Why does IEnumerable's Where pass first argument as "this"
- How does linq Any() works internally
- What does casting an IEnumerable<T> to IEnumerator<T> do
- "Argument data type ntext is invalid for argument 1 of len function" error
- What does the code query.Take(() => 1) do?
- creating Linq to sqlite dbml from DbLinq source code
- Does casting an IEnumerable to an IList<TResult> enumerate the sequence, and why would this be preferred?
- "Argument data type ntext is invalid for argument 1 of upper function" when trying to add a third item to a filter
- Does LINQ convert code to SQL queries
- Statement that works in .NET 3.5 throws "error CS0117: 'System.Linq.Dynamic.DynamicExpression' does not contain a definition for 'ParseLamba'" in 4.5
- why does this linq code get exponentially slower when applying First() to projection?
- CS1061: 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>'
- LINQ - Wildcard on an int column in ASP.NET Core
- How to handle a linq query that returns zero rows and have it return decimal value?
- Why does C# not allow anonymous cast to objects?
- Using ASP.NET and MVC 3, how can I create hidden fields so that a List with an array as a value of each item in the list binds correctly?
- How to omit a value in a select lambda?
- C#: If condition in Linq where clause
- Message LINQ to Entities does not recognize the method System.Object Get(Int32, Int32) method
- how to get data from datatable by ( select and Group By)
- how to execute a nested group aggregate functions with multi groupBy columns using entity framework and linq?
- Returning Dictionary<string,?> from LINQ to SQL
- using multiple IEnumerable Joins
- Join two lists using linq queries - C#
- Linq partial match in list?
- List is empty after linq operation
- Creating UI and database dynamically, what is the best approach?
- How to connect 2 Lists into 1 Model
- Filter large list based on date time
- LINQ to SQL with Extra Nullable Columns
- Same SQL query when run with sp_executesql is fast, very slow if executed as dynamic query in query analyser, why?
- IEquatable(Of T)/IEqualityComparer(Of T) Not Being Called