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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:30:55+00:00 2026-06-01T18:30:55+00:00

Consider the following scenario. I have a method which returns ISomething , but it

  • 0

Consider the following scenario.

I have a method which returns ISomething, but it could be Something or Wrapped<Something>.
I therefore cast the result to Something to use it, but it fails, any help as to why or how to resolve it would be greatly appreciated.

class Program
{
    static void Main(string[] args)
    {
        var a = new DerivedSomething();
        var b = (DerivedSomething)new Wrapped<DerivedSomething>(a); //success
        var c = (DerivedSomething)_GetSomething(false); //success, obsiously!
        var d = (DerivedSomething)_GetSomething(true); //Unable to cast object of type 'test_bed.Wrapped`1[test_bed.DerivedSomething]' to type 'test_bed.DerivedSomething'.
        var e = (DerivedSomething)(ISomething)new Wrapped<DerivedSomething>(a);  //Unable to cast object of type 'test_bed.Wrapped`1[test_bed.DerivedSomething]' to type 'test_bed.DerivedSomething'.

        var works = ((DerivedSomething)_GetSomething(false)).DoSomethingElse(); 
        var fails = ((DerivedSomething)_GetSomething(true)).DoSomethingElse(); //cast exception
    }

    private static ISomething _GetSomething(bool wrap)
    {
        var something = new DerivedSomething();
        return wrap ? new Wrapped<DerivedSomething>(something) : (ISomething)something;
    }
}

public interface ISomething
{
    void DoSomething();
}

public abstract class Something : ISomething
{
    public void DoSomething()
    {
        //some code
    }
}

public class DerivedSomething : Something
{
    public void DoSomething()
    {
        //some code
    }

    public void DoSomethingElse()
    {
        //some code
    }
}

public class Wrapped<T> : ISomething
    where T : ISomething
{
    private readonly T _something;

    public Wrapped(T something)
    {
        _something = something;
    }

    public void DoSomething()
    {
        _something.DoSomething();
    }

    public static explicit operator T(Wrapped<T> wrapped)
    {
        return wrapped._something;
    }
}

It appears that if the type is exposed as the interface when trying to cast, then the operator is not found?

The ‘easy’ solution would be to write a ‘unwrap’ function which optionally unwraps the Wrapped<Something> to Something, but i’d prefer to use operators if possible.

Edit

I think the crux of the problem is: outside of _GetSomething() i wont know whether Something or Wrapped<Something> is going to be returned.

  • 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-01T18:30:57+00:00Added an answer on June 1, 2026 at 6:30 pm

    You cast operator looks like this:

    public static explicit operator T(Wrapped<T> wrapped)
    {
            return wrapped._something;
    }
    

    and you’re casting like

    var d = (Something)_GetSomething(true); //FAILS!
    

    You can not cast to Something , cause Something is a concrete implementation of the T:ISomething. TO make this work you need to write:

    var d = (ISomething)_GetSomething(true); //SUCCESS!
    

    or

    if you really want to use concrete type, you can define generic like:

    public class Wrapped<T> : Something //Something and NOT ISomething 
        where T : ISomething
    {
        .....
        .....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following scenario . I have an array of numbers: [ 1,2,3,4 ]
From couple of days i am thinking of a following scenario Consider I have
Let's consider the following scenario: a function which can generate code colors from white
Consider the following scenario. We have a C++ function with a static local variable:
Let's consider the following scenario. I have the following page where all rendered elements
consider the following scenario: I have a storyboard-based app. I add a ViewController object
Consider the following scenario which I'm due to find myself in shortly: You're to
Consider the following scenario: (The following things have been omitted: Constructors, Getters and Setters,
Consider the following scenario. I have a table (a stupid_table ) in a schema

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.