Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with Covariant return types from an abstract method
    primarykey
    data
    text
    <p>I’m trying to wrap up a two day beat down on Abstract methods and return type Covariance, I’ve already posted two similar questions and I am eternally grateful to the community for the info provided, I just need one last push to get to the finish line. Here is what I am trying to do: 2 abstract classes, RecruiterBase and CandidateBase, both have concreate implementations of RecruiterA and CandidateA. RecruiterBase has an abstract method to get all recruited candidates returning IQueryable. My implementation of RecruiterA overrides the GetCandidates() method to return IQueryable. </p> <pre><code>public abstract class RecruiterBase { // Constructors declared here public abstract IQueryable&lt;CandidateBase&gt; GetCandidates(); } public abstract class CandidateBase { // Constructors declared here } </code></pre> <p>and the implementations:</p> <pre><code>public class CandidateA : CandidateBase { // Constructors declared here } public class RecruiterA : RecruiterBase { // Constructors declared here // ----HERE IS WHERE I AM BREAKING DOWN---- public override IQueryable&lt;CandidateA&gt; GetCandidates() { return from c in db.Candidates where c.RecruiterId == this.RecruiterId select new CandidateA { CandidateId = c.CandidateId, CandidateName = c.CandidateName, RecruiterId = c.RecruiterId }; } } </code></pre> <p>Attempting to complile that throw a compile time error because in my implementation of RecruitreBase the GetCandidates() method returns <code>IQueryable&lt;CandidateA&gt;</code> instead of <code>IQueryable&lt;CandidateBase</code>>.</p> <p>After not being able to get the suggestions from a previous question (<a href="https://stackoverflow.com/questions/1330473/generic-return-types-from-abstract-virtual-methods">Generic return types from abstract/virtual methods</a>) to work, I did a LOT more reading, and came across the following question in SO</p> <p><a href="https://stackoverflow.com/questions/421851/how-to-return-subtype-in-overridden-method-of-subclass-in-c">How to return subtype in overridden method of subclass in C#?</a></p> <p>Which finally made me realize what I had been searching for was a way to implement Covariance for my return type. I used Marc Gravell's snippet...</p> <pre><code>abstract class BaseClass { public BaseReturnType PolymorphicMethod() { return PolymorphicMethodCore();} protected abstract BaseReturnType PolymorphicMethodCore(); } class DerivedClass : BaseClass { protected override BaseReturnType PolymorphicMethodCore() { return PolymorphicMethod(); } public new DerivedReturnType PolymorphicMethod() { return new DerivedReturnType(); } } </code></pre> <p>... as the basis for my solution. So now my RecruiterBase and RecruiterA classes look like:</p> <pre><code>public abstract class RecruiterBase { // Constructors declared here public IQueryable&lt;CandidateBase&gt; GetCandidates() { return GetCandidatesCore(); } public abstract IQueryable&lt;CandidateBase&gt; GetCandidatesCore(); } </code></pre> <p>and my implementation...</p> <pre><code>public class RecruiterA : RecruiterBase { // Constructors protected override IQueryable&lt;CandidateBase&gt; GetCandidatesCore() { return GetCandidates(); } public new IQueryable&lt;CandidateA&gt; GetCandidates() { return from candidates in db.Candidates select new CandidateA { CandidateId = candidates.CandidateId, RecruiterId = candidates.RecruiterId }; } } </code></pre> <p>I was hoping that would finally get me what I was looking for but I got a compile time error in the following code because GetCandidates() cannot implicitly convert CandidateA to CandidateBase:</p> <pre><code> protected override IQueryable&lt;CandidateBase&gt; GetCandidatesCore() { return GetCandidates(); } </code></pre> <p>so I added a cast:</p> <pre><code> protected override IQueryable&lt;CandidateBase&gt; GetCandidatesCore() { return ((IQueryable&lt;CandidateBase&gt;)GetCandidates()); } </code></pre> <p>Everything then compiles but when I actually call GetCandidates() in my controller it returns <code>IQueryable&lt;CandidateBase&gt;</code> instead of <code>IQueryable&lt;CandidateA&gt;</code>. So I am right back where I started. </p> <p>If you made it all the way through this and you can help me I'll send you a 12 pack of your favorite beer!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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