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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:09:29+00:00 2026-06-08T05:09:29+00:00

Suppose we have an interface with a single generic method: public interface IExtender {

  • 0

Suppose we have an interface with a single generic method:

public interface IExtender
{
    T GetValue<T>(string tag);
}

and a simple implementation A of it that returns instances of two different types (B and C) depending on the “tag” parameter:

public class A : IExtender
{
    public T GetValue<T>(string tag)
    {
        if (typeof(T) == typeof(B) && tag == null)
            return (T)(object) new B();
        if (typeof(T) == typeof(C) && tag == "foo")
            return (T)(object) new C();
        return default(T);
    }
}

is it possible to avoid the double cast (T)(object)? Or, is there a way to tell the compiler “hey, I am sure that this cast won’t fail at runtime, just let me do it without first casting to object!”

  • 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-08T05:09:31+00:00Added an answer on June 8, 2026 at 5:09 am

    Or, is there a way to tell the compiler "hey, I am sure that this cast won’t fail at runtime, just let me do it without first casting to object!"

    No, the language is deliberately designed to prevent this. Eric Lippert blogged about this recently. I agree it’s annoying, but it does make a certain kind of sense.

    To be honest, "generic" methods like this are usually a bit of a design smell. If a method has to have special cases for various different types, you should at least consider using separate methods instead. (GetB, GetC)

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

Sidebar

Related Questions

Suppose I have a pure virtual method in the base interface that returns to
Suppose you have a generic interface and an implementation: public interface MyInterface<T> { void
Suppose I have this annotation @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Name { String value(); }
Suppose I have a static method of my class that returns an object of
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>
Suppose I have an Interface with some properties: public interface IDummy { string First
Suppose I have interface and implementation class that implements it and I want to
Suppose I have a few definitions like so: public interface ICategory { int ID
Suppose you have a COM interface ICOMInterface that is implemented by coclasses Coclass1 and
I have a module that uses std::string as part of its interface. I would

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.