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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:48:05+00:00 2026-06-04T00:48:05+00:00

Why is the compiler not able to automatically convert the values in this expression

  • 0

Why is the compiler not able to automatically convert the values in this expression properly?

var input = "Hello";
object x = string.IsNullOrEmpty(input) ? input : DBNull.Value;

//could try this too and get similar compile time error
object x2 = string.IsNullOrEmpty(input) ? 1 : input;

I understand that DBNull.Value cannot be cast to a string; However, I’m curious as to why it cannot be coalesced into an object because the result is just storing a reference. If you place (object) in front of DBNull.Value it will compile and run just fine.

  • 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-04T00:48:06+00:00Added an answer on June 4, 2026 at 12:48 am

    You can fix it with:

    string x = string.IsNullOrEmpty(input) ?
                                           input :
                                           DBNull.Value.ToString();
    

    I have found this excellent explanations in Eric Lippert’s blog post on Type inference woes:

    The specification for the ?: operator states the following:

    The second and third operands of the ?: operator control the type of
    the conditional expression. Let X and Y be the types of the second and
    third operands. Then,

    • If X and Y are the same type, then this is the type of the conditional
      expression.

    • Otherwise, if an implicit conversion exists from X to Y,
      but not from Y to X, then Y is the type of the conditional expression.

    • Otherwise, if an implicit conversion exists from Y to X, but not from
      X to Y, then X is the type of the conditional expression.

    • Otherwise,
      no expression type can be determined, and a compile-time error occurs.

    In this case:

    • string and DBNull aren’t the same type.
    • string doesn’t have an implicit conversion to DBNull
    • DBNull doesn’t have an implicit conversion to string

    So we end up with a compile-time error.

    The compiler doesn’t check what is the type that can "hold" those two types.

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

Sidebar

Related Questions

If the compiler is going to complain that a String object is not initialized
I am not able to get this to work: template<class Input, class Output, class
Why does the C# compiler not even complain with a warning on this code?
I am not overly competent in C++ and this compiler error is just making
In the designer I get this error: Error Creating Control: Object Reference Not Set
My compiler is not able to find the HttpServletRequest getServletContext() method. I am not
Why in code like below is the .NET compiler not able to establish that
I am not able to compile the following program because of cyclic dependency between
volatile is to tell the compiler not to optimize the reference, so that every
I was wondering if there is a possible optimization where the compiler does not

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.