Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 263491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:36:22+00:00 2026-05-11T22:36:22+00:00

I have an IRepository interface that inherits from IRepository<TObject> . I also have a

  • 0

I have an IRepository interface that inherits from IRepository<TObject>. I also have a SqlRepository class that inherits from SQLRepository<TObject>, which in turn implements IRepository<TObject>. Why can’t I instantiate a instance of SqlRepository as an IRepository?

public class MyObject : IObject {
    ...
}

public interface IRepository<TObject> where TObject : IObject, new() {
    ...
}

public interface IRepository : IRepository<MyObject> { }

public class SqlRepository<TObject> : IRepository<TObject> where TObject : IObject, new() {
    ...
}

public class SqlRepository : SqlRepository<MyObject> { }

public class Controller {
    private IRepository _repository;

    public Controller() {
        _repository = new SqlRepository();
    }
}

The example above fails when trying to assign a new SqlRepository to _repository in the Controller class, with the following error message.

Argument '1': cannot convert from 'SqlRepository' to 'IRepository'

Which basic principle of inheritance have I failed to grasp, please help.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:36:22+00:00Added an answer on May 11, 2026 at 10:36 pm

    Because IRepository is an IRepository<MyObject> and not the other way around.

    Basically:

    SqlRepository is a SqlRepository<MyObject>
    which is an IRepository<MyObject>.
    To make that work, you should either inherit IRepository<TObject> from IRepository or make SqlRepository implement IRepository depending in your intention.

    This issue is not generics-specific at all. Assume:

    interface IMovable { }
    interface IDrivable : IMovable { } 
    class Ball : IMovable { }
    

    It’s obvious that Ball is not an IDrivable while it is an IMovable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, you have an IRepository interface. You also have a SqlRepository class, which
I have a class e.g. DerivedClass that inherits from a base class e.g. BaseClass.
I have an interface ( IRepository<T> ) that is currently being extended for each
I have a utility class that takes a constructor parameter of an interface to
If use MongoRepository, You can have following code: @Repository public interface UserRepo extends MongoRepository<User,
I have the following ADO .Net Repository public class Repository : IRepository, IDisposable {
I have a repository like: public class Repository<TEntity> : IRepository<TEntity> where TEntity : class,
I have repository on GitHub to which I commit regularly from my local computer.
Say I have two separate classes, A and B. I also have Repository class
I have a repository which I have already cloned from Subversion. I've been doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.