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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:39:35+00:00 2026-06-15T15:39:35+00:00

a simple question: Is there a syntax for this kind of operation in C#:

  • 0

a simple question:

Is there a syntax for this kind of operation in C#:

  • If condition, return the object itself : default value

Like in below example, but I don’t want to evaluate the myObject twice (which could be some much more complicated thing like database query):

int myVar = (myObject == DBNull.Value ? 0 : (int)myObject);

I know there is syntax for this when checking null (like in below example), but is there for checking any different value, like DBNull.Value in this case?

int myVar = Convert.ToInt32(myObject ?? 0);
  • 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-15T15:39:36+00:00Added an answer on June 15, 2026 at 3:39 pm

    No:

    int myVar = myObject == DBNull.Value ? 0 : (int)myObject;
    

    Is the best you can do. It doesn’t evalutate the object twice just because it is mentioned twice.

    Or wrap it in a function of course:

    int myVar = DBObjectToInt(myObject);
    
    public static int DBObjectToInt(object dbObj)
    {
      return dbObj == DBNull.Value ? 0 : (int)dbObj;
    }
    

    Another option is using Coalesce in your SQL, so you don’t even get a DBNull in the first place.

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

Sidebar

Related Questions

This is a simple question : Is there any automatic indentation alignment on Eclipse
This should be very simple question. There are many programming languages out there, compiled
I know this is a simple question for someone out there, but I have
simple question: I have input fields with inside-labels! Just like the search-box on this
This seems to me like a very simple question, but I can't seem to
This is quite simple question but I didn't find an answer: Is there any
I'm a complete newcomer to this kind of thing, hence the simple question, but
This may seem like a very simple question, however I haven't been able to
Simple question: Is there a class or interface that encapulates the getting of a
Simple question here: is there any way to convert from a jagged array to

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.