Cannot convert type via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

前端 未结 3 700
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 23:14

In C#, if I have a parameter for a function where the parameter type is of an interface, how do a pass in an object that implements the interface.

Here is an example

3条回答
  •  孤街浪徒
    2021-01-08 00:00

    You cannot cast a List of one type to a List of a different type.

    And if you think about it, you would be glad that you can't. Imagine the havoc you could cause if it was possible:

     interface ICustomRequired
     {
     }
    
     class ImplementationOne : ICustomRequired
     {
     }
    
     class ImplementationTwo: ICustomRequired
     {
     }
    
     var listOne = new List();
     var castReference = listOne as List();
     // Because you did a cast, the two instances would point
     // to the same in-memory object
    
     // Now I can do this....
     castReference.Add(new ImplementationTwo());
    
     // listOne was constructed as a list of ImplementationOne objects,
     // but I just managed to insert an object of a different type
    

    Note, however, that this line of code is legal:

     exampleList as IEnumerable;
    

    This would be safe, because IEnumerable does not provide you with any means to add new objects.

    IEnumerable is actually defined as IEnumerable, which means the type parameter is Covariant.

    Are you able to change the parameter of the function to IEnumerable?

    Otherwise your only option will be to create a new List.

    var newList = (exampleList as IEnumerable).ToList();
    

    or

    var newList = exampleList.Cast().ToList();
    

提交回复
热议问题