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 6053959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:05:56+00:00 2026-05-23T08:05:56+00:00

I have a much more complicated issue, but I’ve boiled it down to the

  • 0

I have a much more complicated issue, but I’ve boiled it down to the following simple example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            IFactory<IProduct> factory = new Factory();
        }
    }

    class Factory : IFactory<Product>
    {

    }

    class Product : IProduct
    {

    }

    interface IFactory<T> where T : IProduct
    {

    }

    interface IProduct
    {

    }
}

All is well and dandy… except that I get this error.

Error 1 Cannot implicitly convert type Sandbox.Factory to Sandbox.IFactory<Sandbox.IProduct>. An explicit conversion exists (are you missing a cast?) c:\~~\Program.cs 12 42 Sandbox

Anyone willing to provide insight into why this is the case? I’m sure Jon Skeet or Eric Lippert could explain in a heartbeat why this is, but there has to be someone that not only understands WHY this can’t be inferred, but can explain how best to solve this situation.

Follow up question here

  • 1 1 Answer
  • 0 Views
  • 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-23T08:05:56+00:00Added an answer on May 23, 2026 at 8:05 am

    It is because Factory is an IFactory< Product> and what you are assigning it to is a IFactory< IProduct>, and since IFactory is not covariant, you cannot cast a generic of subtype to a generic of supertype.

    Try making IFactory< out T>, which should make the following assignment work.

    EDIT:

    @Firoso, in your factory interface you are trying to create a list, in which you can write to. If your interface is covariant you can not write to anything, because of the following:

    List<object> list = new List<string>(); //This is not possible by the way
    list.Add(new {}); //Will fail here because the underlying type is List<string> 
    

    You should ignore covariance in your case and just create assign to an IFactory<Product>instead or change factory to inherit IFactory<IProduct>instead, I recommend the latter but it is up to you

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

Sidebar

Related Questions

I have a very simple query that is not much more complicated than: select
This might be a simple one, but since I don't have much knowledge about
I don't have much experience working with resultsets, but as ResultSet is an interface,
I don't have much experience with Serial I/O, but have recently been tasked with
What does x :: xs' mean? I dont have much functional experience but IIRC
i need horizontal scrolling script in javascript, but don't have much time to write
I currently have interfaces much like the following: interface IService { void Start(); IHandler
My actual implementation of this is much more complicated, with authentication and a bunch
I have the following situation (simplified): a.h: #include <boost/serialisation/serialisation.hpp> class B; using namespace std;
This post on SO answers most of the questions I have (much thanks to

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.