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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:07:00+00:00 2026-06-12T16:07:00+00:00

So I know that this works: class A { } class B : A

  • 0

So I know that this works:

class A
{
}
class B : A
{       
}

[Test]
public void CanCast()
{
    Assert.That(typeof(A).IsAssignableFrom(typeof(B)));
    Assert.That(!typeof(B).IsAssignableFrom(typeof(A)));
}

However, let’s say those two types were Int32 and Int64.

At runtime, I can cast an Int32 value to an Int64 variable, but not the other way around. How can I check this kind of casting-compatibility at runtime? (IsAssignableFrom doesn’t work for this, it always gives false for Int32 and Int64)

EDIT: I cannot simply try to cast, because I don’t have any value of those types, I’m asking the hypothetical scenario of having two types A and B, not having two values a and b.

  • 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-12T16:07:01+00:00Added an answer on June 12, 2026 at 4:07 pm

    For non-primitive types, you can reflect and inspect whether there is an op_Implicit method on either type supporting the conversion. IL doesn’t actually support true operator overloading, so it’s purely a convention system for C# to recognize the operator overloads. The method will also be marked IsSpecialName if it was created from an operator overload definition in C#.

    For primitive types (like Int32 and Int64), the simplest option is to hardcode the various cases, as conversion is via a primitive IL opcode rather than via a method. There are only a handful of primitive types, though, so it wouldn’t be difficult to create a method with the check for all possibilities for each primitive type.

    One side note since your example mentioned primitive value types specifically, note that the existence of an implicit ‘conversion’ (in C#) does not mean that all ‘casts’ will work. The C# cast operation (T)x can also mean ‘unbox the value in x to type T’. If x contains a boxed Int32 and you attempt to (Int64)x, this will fail at runtime, even though you can implicitly ‘convert’ an Int32 to an Int64. See Eric Lippert for more information as to why unboxing works this way.

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

Sidebar

Related Questions

I have written this piece of code public class Test{ public static void main(String[]
This works: <cfdirectory action=list name=qry directory=C:\inetpub\wwwroot\Atlas\ > </cfdirectory> <cfdump var=#qry#> So I know that
I have a Java class that looks like this: public class My_ABC { int
I know that this sort of question has been asked here before, but still
I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'
I know that this question must have been asked and answered a million times,
I know that this is a newbie question but I am a newbie so
I know that this question had already been asked a couple of times before,
I know that this has already been asked here but the answer (using a
I know that this can be easily done by using if(i%5 == 0 OR

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.