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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:58:30+00:00 2026-06-06T13:58:30+00:00

I have gotten myself into a catch 22 and cannot seem to find my

  • 0

I have gotten myself into a catch 22 and cannot seem to find my way out. I’m trying to implement a simple [Service Locator][1], represented by the GetInstance-method in the example code below. Now the problem is I get a compiler error on the return statement saying:

Cannot implicitly convert type 'Cyberspace.SubClass' to 'Cyberspace.BaseClass<T>'

I’ve tried rewriting so that the SubClass is a generic class itself, but then I get compiler error on the return statement in the DoSomething-method.

Can anyone make this code compile, retaining the Service Locator pattern principles which allow me to have an abstract return type in the GetInstace-method? Or am I trying to acheive something impossible here?

namespace Cyberspace
{
    class BaseClass<T>
    {
        BaseClass<T> GetInstance() 
        { 
            return new SubClass();
        }
        virtual T DoSomething() { return default(T); }          
    }

    class SubClass : BaseClass<OtherClass>
    {
        public override OtherClass DoSomething()
        { 
            var other = new OtherClass { Description = "Generics are hard"};
            return other;
        }
    }

    class OtherClass
    {
        internal string Description { get; set; }
    }
}

Attempt 2:

namespace Cyberspace
{
    class BaseClass<T>
    {
        static BaseClass<T> GetInstance() // The "Service Locator" method
        { 
            return new SubClass<T>(); 
        }
        internal virtual T DoSomething() { return default(T); }        
    }

    class SubClass<T> : BaseClass<T> where T: OtherClass
    {
        internal override T DoSomething()
        { 
            var other = new OtherClass { Description = "Generics are hard"};
            return (T) other;
        }
    }

    class OtherClass
    {
        internal string Description { get; set; }
    }
}

This yields the following error on line return new SubClass<T>();

The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Cyberspace.SubClass<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'Cyberspace.OtherClass'

  • 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-06-06T13:58:32+00:00Added an answer on June 6, 2026 at 1:58 pm

    When using a type parameter T you declare: “I don’t care what the type actually is. I just need some type, any type at all. Call it T.”. But in your case it looks like you want a specific type. No need for generics, then.

    class SubClass : BaseClass<OtherClass>
    {
        internal override OtherClass DoSomething()
        { 
            var other = new OtherClass { Description = "Generics are hard"};
            return other;
        }
    }
    

    Why doesn’t the following work?

            var other = new OtherClass { Description = "Generics are hard"};
            return (T) other;
    

    Because T might be derived from OtherClass. You cannot convert OtherClass to a class derived from OtherClass. Makes no sense and the compiler is smart enough to catch it.


    The discussion in the comments lead to the following workaround:

    return (BaseClass<T>)(object)new SubClass();
    

    Of course, this only works, if the types line up at runtime.

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

Sidebar

Related Questions

I have gotten myself into a tangled mess with quotes. I am trying to
I've seemed to have gotten myself into a bit of a pickle. I'm trying
I've been playing with XCode4 and seem to have gotten myself in a bit
I'm just beginning Android and have gotten myself stuck. I've made a simple app/view.
I'm new to Java and have gotten myself into a situation where it's evident
I seem to have gotten confused as to what the css clear keyword means.
I'm sure this is very simple. I have gotten colorbox to work before, but
I'm working on learning Objective-C/Coaoa, but I've seem to have gotten a bit stuck
One of the practices I have gotten into in Python from the beginning is
So have gotten to record in my F# journey and at first they seem

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.