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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:09:38+00:00 2026-05-26T13:09:38+00:00

While reading Microsoft documentation, I stumbled on such an interesting code sample: interface ISomeInterface

  • 0

While reading Microsoft documentation, I stumbled on such an interesting code sample:

interface ISomeInterface
{...}
class SomeClass
{...}
class MyClass<T> 
{
   void SomeMethod(T t)
   {
      ISomeInterface obj1 = (ISomeInterface)t;//Compiles
      SomeClass      obj2 = (SomeClass)t;     //Does not compile
   }
}

It means you can cast your generic to the interface explicitly but not to the class unless you have a constraint. Well, I still cannot understand the logic behind the decision as both interface and class type castings are throwing exceptions, so why would one protect against only one of these exceptions?

BTW- there is a way around the compile error but this does not remove the logic mess in my head:

class MyOtherClass
{...}

class MyClass<T> 
{

   void SomeMethod(T t)

   {
      object temp = t;
      MyOtherClass obj = (MyOtherClass)temp;

   }
}
  • 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-26T13:09:38+00:00Added an answer on May 26, 2026 at 1:09 pm

    That’s exactly what you get in normal circumstances – without generics – when you try to cast between classes with no inheritance relationship:

     public interface IA
     {
     }
    
     public class B
     {
     }
    
     public class C
     {
     }
    
     public void SomeMethod( B b )
     {
         IA o1 = (IA) b;   <-- will compile
         C o2 = (C)b;  <-- won't compile
     }
    

    So without a constraint, the generic class will behave as if there is no relationship between the classes.

    Continued…

    Well, let’s say someone does this:

     public class D : B, IA
     {
     }
    

    And then calls:

    SomeMethod( new D() );
    

    Now you’ll see why the compiler lets the interface cast pass. It really can’t know at compile time if an interface is implemented or not.

    Remember that the D class may very well be written by someone who is using your assembly – years after you compiled it. So there is no chance that the compiler can refuse to compile it. It must be checked at run time.

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

Sidebar

Related Questions

While reading the OWASP site, I stumbled upon pages www.owasp.org/index.php/Category:Countermeasure and www.owasp.org/index.php/Category:Control . My
While reading a book about JavaScript I stumbled across an example: var names =
While reading a code I came across, the following definition and initialization of a
Error while reading Excel sheet from C#: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source
While reading the Doctrine documentation , I found this example: /** * @Entity *
While reading the documentation on Python re module I decided to have a look
While reading through another question here, on creating a URL shortening service, it was
While reading the answers to this question I got a doubt regarding the default
While reading cocoa tutorials i've noticed that some of the tutorials use AppDelegate and
While reading proggit today, I came upon this comment in a post about how

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.