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

  • Home
  • SEARCH
  • 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 1037459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:51:15+00:00 2026-05-16T14:51:15+00:00

This is a rather elementary C# question; my apologies, but I haven’t been able

  • 0

This is a rather elementary C# question; my apologies, but I haven’t been able to find this elsewhere.

What is the best way to convert from Object<class> to Object<interface>?

I.e.

//fake interface
interface ignu {}

//fake class which implements fake interface
class bee : ignu {}

//function which accepts a generic with the interface
function bang(template<bee>) {}

//template type but with a class that implements the interface
template<bar> foo;

//trying to call the function, but compiler complains it can't implicitly convert
bang(foo); 

//compilers complains that it cannot convert from template<bee> to template<ignu>
bang((template<ignu>)bee)

Maybe I’m way off base, but this seems like it should work and be doable, but the solution is escaping me.

Edit: Changed mygen to be template as I was using both to refer to the same thing which was confusing

Edit: I ended up using a Cast similar to:
bang(bee.Cast());

  • 1 1 Answer
  • 2 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-16T14:51:16+00:00Added an answer on May 16, 2026 at 2:51 pm

    .NET 3.5 doesn’t have Covariance and Contravariance in Generics as MonkeyWrench indicated. However, there are built in ways to do the casting necessary.

    class Program
    {
        static void Main(string[] args)
        {
            List<ccc> c = new List<ccc>();
            c.Add(new ccc());
    
            List<iii> i = new List<iii>(c.Cast<iii>());
        }
    }
    
    interface iii
    {
        void DoIt();
    }
    
    class ccc : iii
    {
        public void DoIt()
        {
            throw new NotImplementedException();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched google but couldn't find an answer to this rather simple question. I
I've looked through several posts on StackOverflow, but haven't been able to find an
This is a rather vague question but here we go - I would like
I've been able to accomplish this in other languages, but am having trouble in
Please forgive this rather basic question, but I'm very new to Java and still
I haven't worked much with remoting so excuse this rather rudimentary question, If I
This question may have been answered on SO several times but it didn't cover
I've been having this rather frustrating problem, and having failed so many times, I'm
I know this is rather laughable, but I can't seem to get simple C++
This may be rather noobish but I'm gonna ask anyhow. I have a class

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.