Note that there are some explanatory texts on larger screens.

plurals
  1. PO'The type arguments cannot be inferred from the usage'
    text
    copied!<p>I am having some troubles with generics and inheritance. I have an abstract class called <code>CriteriaBase</code> whose job it is to determine if an entity <code>T</code> matches the criteria defined in any sub-classes. The sub-classes have to implement a method which returns a <code>Func</code> representing the criteria. The problem arises when I try to use generics for the <code>Func</code>. Hopefully some code will illustrate my problem.</p> <pre><code>public abstract class CriteriaBase&lt;T, U&gt; where T : ICrossoverable where U : IChromosome { protected abstract Func&lt;U, bool&gt; Criteria { get; } //some code removed for brevity private int GetNumberOfCriteriaMatches(T season) { //1. works //Func&lt;IChromosome, bool&gt; predicate = c =&gt; c.Genes == null; //return season.Chromosomes.Count(predicate); //2. doesn't work - The type arguments for method 'int //System.Linq.Enumerable.Count&lt;TSource&gt;(this IEnumerable&lt;TSource&gt;, //Func&lt;TSource, bool&gt;)' //cannot be inferred from the usage. Try specifying the type arguments //explicitly. return season.Chromosomes.Count(Criteria); } } </code></pre> <p>My intention is that the <code>CriteriaBase</code> class should be generic and completely reusable.</p> <p>An example sub-class:</p> <pre><code>public class TopTeamsPlayingEachOtherCriteria : CriteriaBase&lt;Season, MatchDay&gt; { //some code removed for brevity protected override Func&lt;MatchDay, bool&gt; Criteria { get { return matchDay =&gt; matchDay.Fixtures.Count( fixture =&gt; fixture.HomeTeam.TableGrouping.Ordering == 1 &amp;&amp; fixture.AwayTeam.TableGrouping.Ordering == 1) &gt; 1; } } } </code></pre> <p>The problem is in the <code>GetNumberOfCriteriaMatches()</code> method. Option 2 is how I originally wrote the code but I get the compile error as listed. If I use option 1 then the code compiles but it means that when I override <code>Criteria</code> in the sub-class, I have to use <code>IChromosome</code> instead of <code>MatchDay</code> which doesn't work (I need to access specific features of a <code>MatchDay</code>). In my simple mind, options 1 and 2 are equivalent. Option 2 simply replaces <code>IChromosome</code> with a generic type <code>U</code> which is restricted to a class that implements <code>IChromosome</code>.</p> <p>Is what I'm trying to achieve possible? If so, what am I missing/misunderstanding? If not, how should I approach this problem?</p> <p>For completeness (included at the end as I'm not sure how much it helps with the question), here are the two entities that I'm currently using for <code>T</code> (<code>Season</code>) and <code>U</code> (<code>MatchDay</code>).</p> <pre><code>public class Season : ICrossoverable { private readonly IEnumerable&lt;MatchDay&gt; _matchDays; public Season(IEnumerable&lt;MatchDay&gt; matchDays) { _matchDays = matchDays; } public IEnumerable&lt;MatchDay&gt; MatchDays { get { return _matchDays; } } //ICrossoverable implementation public IEnumerable&lt;IChromosome&gt; Chromosomes { get { return _matchDays; } } } public class MatchDay : IChromosome { private readonly int _week; private readonly List&lt;Fixture&gt; _fixtures; public MatchDay(int week, List&lt;Fixture&gt; fixtures) { _week = week; _fixtures = fixtures; } //some code removed for brevity public IEnumerable&lt;Fixture&gt; Fixtures { get { return _fixtures; } } //IChromosome implementation public IEnumerable&lt;IGene&gt; Genes { get { return Fixtures; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload