Compiler Error: Cannot convert from 'List<string>' to 'IList<object>'

Lee Grissom

How would I change the following code to make it compile? (Other than casting/changing strings to List<object>).

Action<IList<object>> DoSomething = (list) => { /*list is never modified*/ };
var strings = new List<string>() { "one", "two" };
DoSomething(strings);
p.s.w.g

As the compiler error indicates, you can't cast a IList<string> to an IList<object>. This is because the IList<T> interface is invariant with respect to T. Imagine if your did something like this:

Action<IList<object>> DoSomething = (list) => list.Add(1);

This would be valid with a IList<object> but not with an IList<string>.

As long as you're not trying to modify the collection, a simple solution is to change the IList<T> to IEnumerable<T>, which is covariant with respect to T:

Action<IEnumerable<object>> DoSomething = (list) => { };
var strings = new List<string>() { "one", "two" };
DoSomething(strings);

Further Reading

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert IList<Object> to a List<String>

From Dev

Cannot Convert IList To Binding List

From Dev

Cannot Convert IList To Binding List

From Dev

Convert IList<IList<object>> to List<List<object>> form

From Dev

Convert IList<IList<object>> to List<List<object>> form

From Dev

cannot convert from 'object' to 'string'

From Dev

Cannot convert from object to string

From Dev

Argument1: cannot convert from 'string' to 'int' error in List

From Dev

issue with java 8 collectors Type mismatch: cannot convert from List<Object> to List<String>

From Dev

Weird compiler error: Cannot convert parameter from 'int' to 'int &&'

From Dev

ASP.NET MVC cannot convert IList to IEnumerable? Also tried with IList to IList and still an error

From Dev

Get keys from IList<IDictionary<string, object>>

From Dev

vb.net Cannot convert Dictionary(Of String, List(Of String)) to Object

From Dev

Cannot convert source type 'List<Person>' to IList<ISomething>

From Dev

Can not convert IList to List

From Dev

cannot convert from list<object> to list<callable<type>> java

From Dev

How can I convert IList<object> to string array?

From Dev

How can I convert IList<object> to string array?

From Dev

Cannot convert from 'string' to 'system.collections.generic.list string'

From Dev

How to create a list of objects from single object in object of IList

From Dev

Type mismatch: cannot convert from element type Object to List

From Java

Creating a comma separated list from IList<string> or IEnumerable<string>

From Dev

Cannot convert expanded class to IList

From Dev

Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string'

From Dev

type mismatch cannot convert from element type object to string

From Dev

Java - Type mismatch: cannot convert from element type Object to String

From Dev

spark Type mismatch: cannot convert from JavaRDD<Object> to JavaRDD<String>

From Dev

type mismatch cannot convert from element type object to string

From Dev

Java - Type mismatch: cannot convert from element type Object to String

Related Related

  1. 1

    Convert IList<Object> to a List<String>

  2. 2

    Cannot Convert IList To Binding List

  3. 3

    Cannot Convert IList To Binding List

  4. 4

    Convert IList<IList<object>> to List<List<object>> form

  5. 5

    Convert IList<IList<object>> to List<List<object>> form

  6. 6

    cannot convert from 'object' to 'string'

  7. 7

    Cannot convert from object to string

  8. 8

    Argument1: cannot convert from 'string' to 'int' error in List

  9. 9

    issue with java 8 collectors Type mismatch: cannot convert from List<Object> to List<String>

  10. 10

    Weird compiler error: Cannot convert parameter from 'int' to 'int &&'

  11. 11

    ASP.NET MVC cannot convert IList to IEnumerable? Also tried with IList to IList and still an error

  12. 12

    Get keys from IList<IDictionary<string, object>>

  13. 13

    vb.net Cannot convert Dictionary(Of String, List(Of String)) to Object

  14. 14

    Cannot convert source type 'List<Person>' to IList<ISomething>

  15. 15

    Can not convert IList to List

  16. 16

    cannot convert from list<object> to list<callable<type>> java

  17. 17

    How can I convert IList<object> to string array?

  18. 18

    How can I convert IList<object> to string array?

  19. 19

    Cannot convert from 'string' to 'system.collections.generic.list string'

  20. 20

    How to create a list of objects from single object in object of IList

  21. 21

    Type mismatch: cannot convert from element type Object to List

  22. 22

    Creating a comma separated list from IList<string> or IEnumerable<string>

  23. 23

    Cannot convert expanded class to IList

  24. 24

    Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string'

  25. 25

    type mismatch cannot convert from element type object to string

  26. 26

    Java - Type mismatch: cannot convert from element type Object to String

  27. 27

    spark Type mismatch: cannot convert from JavaRDD<Object> to JavaRDD<String>

  28. 28

    type mismatch cannot convert from element type object to string

  29. 29

    Java - Type mismatch: cannot convert from element type Object to String

HotTag

Archive