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

  • Home
  • SEARCH
  • 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 458491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:40:37+00:00 2026-05-12T22:40:37+00:00

I have a number of methods doing next: var result = command.ExecuteScalar() as Int32?;

  • 0

I have a number of methods doing next:

var result = command.ExecuteScalar() as Int32?;
if(result.HasValue)
{
   return result.Value;
}
else
{
   throw new Exception(); // just an example, in my code I throw my own exception
}

I wish I could use operator ?? like this:

return command.ExecuteScalar() as Int32? ?? throw new Exception();

but it generates a compilation error.

Is it possible to rewrite my code or there is only one way to do that?

  • 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-12T22:40:37+00:00Added an answer on May 12, 2026 at 10:40 pm

    For C# 7

    In C# 7, throw becomes an expression, so it’s fine to use exactly the code described in the question.

    For C# 6 and earlier

    You can’t do that directly in C# 6 and earlier – the second operand of ?? needs to be an expression, not a throw statement.

    There are a few alternatives if you’re really just trying to find an option which is concise:

    You could write:

    public static T ThrowException<T>()
    {
        throw new Exception(); // Could pass this in
    }
    

    And then:

    return command.ExecuteScalar() as int? ?? ThrowException<int?>();
    

    I really don’t recommend that you do that though… it’s pretty horrible and unidiomatic.

    How about an extension method:

    public static T ThrowIfNull(this T value)
    {
        if (value == null)
        {
            throw new Exception(); // Use a better exception of course
        }
        return value;
    }
    

    Then:

    return (command.ExecuteScalar() as int?).ThrowIfNull();
    

    Yet another alternative (again an extension method):

    public static T? CastOrThrow<T>(this object x) 
        where T : struct
    {
        T? ret = x as T?;
        if (ret == null)
        {
            throw new Exception(); // Again, get a better exception
        }
        return ret;
    }
    

    Call with:

    return command.ExecuteScalar().CastOrThrow<int>();
    

    It’s somewhat ugly because you can’t specify int? as the type argument…

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

Sidebar

Related Questions

I have a number of existing methods within a controller. Each one of those
So I have a really long chain of methods, something similar to: return self.append_command(fbghasjfa).append_command(input_file_part).append_command(output_video_codec_part).append_command(output_resolution_part).append_command(output_video_bitrate_part).append_command(strict_part).append_command(output_audio_codec_part).append_command(output_number_of_audio_channels_part).append_command(output_audio_bitrate_part).append_command(output_file_part).__finalized
This question might have asked here number of times . After doing some google
I have 6 elements on my page. A priority number is displayed next to
I have a method: public void StoreNumberInSmallestType(ValueType number) { if (numberTypes == null) numberTypes
I have and old(ish) C# method I wrote that takes a number and converts
I have a method connection(int n) which gives me all the cells number that
Say I want to have a method that takes any kind of number, is
I have number of line breaks in a string. But I only want it
I'm stuck with the following scenario and appreciate any help/advice.. Requirement I have number

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.