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

The Archive Base Latest Questions

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

Beyond the regular boring difference between Cast and As if i know that apple

  • 0

Beyond the regular boring difference between Cast and As

  • if i know that apple is a Fruit so I can use (Fruit)apple – and it throws an exception if it aint
  • as value can be checked against null to see if succeeded [won’t throw Exception…]

However Ive been reading @EricLippert article about this and there was a nice sample about Nullable Value Types :

short? s = (short?)123;
int? i = s as int?;

this won’t compile…

Cannot convert type ‘short?’ to ‘int?’ via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

Fine.

so why this :

    short? s = (short?)123;
    int? i = (int?)s;

Does Compile ? ( Against ALL Expectations ! I KNOW that s is not int? – and it should go BANG but it aint …)

the Cast checking here should be much more deadly than the former example (which went Bang)

I feel bad asking about this much-talked subject.

Thanks in Advance.

  • 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-01T10:55:48+00:00Added an answer on June 1, 2026 at 10:55 am

    In your first example, the as operator attempts to use the object s as an int?. Since int? isn’t anywhere in the inheritance chain of short?, this operation fails.

    In your second example, you’re actually creating a new int? i with the value from short? s. This is a more generous operation, because it doesn’t have to preserve the original s object on the left hand side.

    The important point here is that as isn’t allowed to do anything that doesn’t preserve your object’s identity. An explicit cast can.

    Here’s what the C# standard says about how the (int?) form works:

    6.1.4 Implicit nullable conversions

    Predefined implicit conversions that operate on non-nullable value
    types can also be used with nullable forms of those types. For each of
    the predefined implicit identity and numeric conversions that convert
    from a non-nullable value type S to a non-nullable value type T, the
    following implicit nullable conversions exist:

    · An implicit conversion from S? to T?.

    · An implicit conversion from S to T?.

    Evaluation of an implicit nullable conversion based on an underlying
    conversion from S to T proceeds as follows:

    · If the nullable conversion is from S? to T?:

    o If the source value is null (HasValue property is false), the
    result is the null value of type T?.

    o Otherwise, the conversion is evaluated as an unwrapping from S? to
    S, followed by the underlying conversion from S to T, followed by a
    wrapping (§4.1.10) from T to T?.

    · If the nullable conversion is from S to T?, the conversion
    is evaluated as the underlying conversion from S to T followed by a
    wrapping from T to T?.

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

Sidebar

Related Questions

I understand that NSNumber cannot go beyond 1/10ths place. What can I use as
I'm looking for any beyond basic resources that can help me to be proficient
I've been looking for list of types of queries that geocoder supports beyond regular
I've seen a few comments here that mention that modern regular expressions go beyond
I don't really know math well beyond Algebra 1, however I can usually hack
In Android, what functionality do ListActivity and ListView provide beyond a regular Activity and
I'm a graphic designer who is trying to use R to create graphs that
When looking beyond the RAD (drag-drop and configure) way of building user interfaces that
For some reason beyond me I can't access the mysql server on a machine.
I have a web application that registered users use. These users have good email

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.