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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:33:31+00:00 2026-05-12T15:33:31+00:00

I created the following property, which threw an InvalidCastException if the getter was accessed

  • 0

I created the following property, which threw an InvalidCastException if the getter was accessed when ViewState[TOTAL_RECORD_COUNT] was null.

public long TotalRecordCount
{
    get { return (long)(ViewState[TOTAL_RECORD_COUNT] ?? -1); }
    set { ViewState[TOTAL_RECORD_COUNT] = value; }
}

My thought is that it incorrectly attempted to unbox the object in ViewState[TOTAL_RECORD_COUNT] to an int, which failed because it contained a long, but I think there might be a flaw in that logic. I will leave it as an exercise to the reader to point out that flaw.

I have since changed that property to read

public long TotalRecordCount
{
    get { return (long?)ViewState[TOTAL_RECORD_COUNT] ?? -1; }
    set { ViewState[TOTAL_RECORD_COUNT] = value; }
}

which works just swell. Still, I am left wondering what was wrong with my original version… StackOverflow to the rescue?

Note that if i try to execute (long)(ViewState[TOTAL_RECORD_COUNT] ?? -1) in the Immediate Window, I get the error message Cannot unbox 'ViewState[TOTAL_RECORD_COUNT] ?? -1' as a 'long' and if I execute (ViewState[TOTAL_RECORD_COUNT] ?? -1).GetType().Name I get Int32. I can execute (long)-1 and end up with -1 as an Int64…so what’s up?

  • 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-12T15:33:31+00:00Added an answer on May 12, 2026 at 3:33 pm

    The return type of ViewState indexer is Object (I assume you mean ASP.NET viewstate here). Now consider what the compiler has to do when it sees this (which is equivalent to your code):

    object o = ViewState[...];
    var x = o ?? -1;
    

    It has to deduce the result type of expression o ?? -1 somehow. On the left it sees an object, on the right is an int. Clearly, the most general type for this expression is also object. However, this means that if it actually ends up using that -1 (because o was null), it will have to convert it to object – and for an int, this means boxing.

    So x is of type object, and it may contain an int (and maybe also some other integral type – we don’t know what is in your viewstate, it could be short, for example). Now you write:

    long y = (long)x;
    

    Since x is object, this is unboxing. However, you can only unbox value types into exact same type (with the only exception being that you can substitute a signed type for an equivalent unsigned type, and enum for its underlying base type). That is, you cannot unbox int into long. A far simpler way to repro this, with no “extra” code, would be:

    object x = 123;
    long y = (long)x;
    

    Which also throws InvalidCastException, and for exact same reason.

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

Sidebar

Related Questions

I created the following class located in the MainJPrint.java file import com.XXXXX.pdfPrint.PDFPrint; public class
I created the following method for retrieving stored settings from the database: public String
I created the following interface: <?php interface Action { public function execute(\requests\Request $request, array
I've created a custom control with, amongst others, the following: public partial class MyButton
Please consider the following Amount value type property which is marked as a nullable
The following code works, but I'm wondering if the MemoryStream created is closed properly.
I have created following rule in the .htaccess file located at the root of
I have created following thing in android using android compatibility support package Basically i
I created the following function to simplify a piece of particularly complex code. CREATE
I created the following script to query a table and return the first 30

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.