Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a interface class:

C#
public interface IStatFunction : IComparable<IStatFunction>
    {
        
    }


and normal class extends IStatFunction interface:
C#
public class StatFunction : IStatFunction
    {
        
    }


Now my code will use both of above class and interface:

C#
public void addEffect(IStatOwner statOwner, List<IStatFunction> functions)
        {
            
        }


other code call this addEffect function:
C#
List<StatFunction> allModifiers = null;
            cgs.addEffect(item, allModifiers);


Now i get error: Cannot convert List<statfunction> to List<istatfunction>

so how can i fix this error?

What I have tried:

Im trying to use ConvertAll, its can remove error but i dont know its work or not:
C#
cgs.addEffect(item, allModifiers.ConvertAll(o => (IStatFunction)o));
Posted
Updated 18-Dec-16 2:40am
v2

1 solution

It's complicated as to why - Covariance and contravariance (computer science) - Wikipedia[^] helps to explain it - but it's simple to fix:
C#
public void addEffect<T>(IStatOwner statOwner, List<T> functions) where T : IStatFunction
    {
    ...    
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900