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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:44:37+00:00 2026-05-30T20:44:37+00:00

UPDATE: This isn’t about getting it to compile. The question is, why does the

  • 0

UPDATE: This isn’t about getting it to compile. The question is, why does the C# compiler allow the cast when using an interface, but it can’t figure out the type when I use a class that implements the same interface.

I am getting the following error:

Cannot convert type 'Amber.BLL.iWeb.Session.AppSession' to 'TService'   

Here is the code:

public override TService GetService<TService>() 
{
    if ( typeof( TService ) == typeof( IAppSession ) )
    {
        AppSession session = new AppSession();
        return (TService) session;
    }
    throw new Exception( String.Format( 
        "iWebFactoryProvider cannot create services of type '{0}'.", 
        typeof( TService ).Name ) );
}

As it so happens, the AppSession class implements the IAppSession interface. If I change the line of code that instantiates AppSession to use the interface, like this:

IAppSession session = new AppSession();

suddenly everything compiles fine. I also note that it compiles fine if I do this:

AppSession session = new AppSession();
return (TService) (IAppSession) session;

In case it matters, the GetService() is overriding a method whose signature is declared like this:

public virtual TService GetService<TService>() where TService : class

In short, I can’t figure out what the rules should be here so I can know how to avoid this situation in the future. Why was the compiler happy to cast the interface, but not happy to cast the interface’s implementing class?

I note that this question is asking about a similar issue, but the answer isn’t detailed enough for me to understand how it applies to my situation.

  • 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-30T20:44:39+00:00Added an answer on May 30, 2026 at 8:44 pm

    Why does the C# compiler allow the cast when using an interface, but it can’t figure out the type when I use a class that implements the same interface?

    Good question. Consider the following:

    public interface I {}
    public class D {} // Note that D does not even implement I!
    public class E
    {
        public static M<T>(T t)
        {
            D d1 = (D)t; // Illegal
            D d2 = (D)(object)t; // Legal
            D d3 = (D)(I)t; // Legal
        }    
    }
    

    Let’s break your question up into three questions.

    Why is the cast directly from T to D illegal?

    Suppose it were legal. Then E.M<D>(new D()) would work just fine; we’d cast the T to D and in fact it is a D, so no problem.

    Now suppose we create an entirely different assembly with:

    class C 
    {
        public static explicit operator D(C c) { whatever }
    }
    

    And you call E.M<C>(new C()) in that assembly.. What do you reasonably expect to happen? You have an object of type C, it is being cast to D, and there is an explicit conversion operator right there from C to D. Most people would reasonably expect that the explicit conversion operator would be called.

    But how on earth is the compiler supposed to realize when compiling the body of M that someone in the future might create a class C in a completely different assembly? The compiler has no way to emit the call to the conversion operator when compiling M. So we have three choices:

    1. Make cast operators sometimes use explicit conversion operators and sometimes not, depending on whether you’re in a generic or not.
    2. Make cast operators start the compiler again at runtime to look for explicit conversion operators that might have been added in different assemblies after the original code was compiled.
    3. Disallow the cast in the first place.

    In short, our choices are (1) make generics inconsistent, (2) make generics slow and unpredictable, or (3) disallow a feature that is already working against genericity. This is an easy choice to make; we chose (3).

    If you want (2), you can have it in C# 4; dynamic starts the compiler again at runtime and works out whether there is an explicit conversion operator.

    Why is the cast indirectly from T to D via object legal?

    Because now no user-defined conversion can be relevant; there is never a user-defined conversion from object to anything.

    Why is the cast indirectly from T to D via I legal?

    Because now no user-defined conversion can be relevant; there is never a user-defined conversion from an interface to anything.

    Bonus question:

    But D does not even implement I! What’s up with that?

    A derived class of D might:

    class F : D, I {}
    ...
    E.M<D>(new F());
    

    Now t can be cast to I because it might implement I, and I can be cast to D because it might be F.

    If D were sealed then it would not be legal to cast from I to D because then there could not possibly be a derived F type.

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

Sidebar

Related Questions

This isn't exactly a development question, but it does affect my productivity! Recently, installing
UPDATE: This question is out of date, but left for informational purposes. Original Question
Update: This question was an epic failure, but here's the working solution. It's based
No this isn't a copy of this question: Button in update panel is doing
I know this isn't a programming question, but since a lot of people here
Update: this question, including the title, was rephrased, see history for details I know
Update: This does work, I was being stupid :( i have the following extension
Update: This is, as I was told, no principle Python related problem, but seems
Update: This question is a duplicate of Are there any programming languages targeting PHP,
Can anybody help me understand why this update query isn't updating the fields in

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.