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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:26:18+00:00 2026-05-26T01:26:18+00:00

The following compiles in VB.NET (with Option Strict On) and outputs False : Dim

  • 0

The following compiles in VB.NET (with Option Strict On) and outputs False:

Dim b As Boolean? = Nothing
Dim myString = If(b, "True", "False")

Why does that work?

  • The documentation clearly states that the three-argument version of If requires a Boolean as the first parameter:

    argument1 Required. Boolean. Determines which of the other arguments to evaluate and return.

  • and there is no implicit conversion from Boolean? to Boolean:

    Dim b1 As Boolean? = Nothing
    Dim b2 As Boolean = b1   ' Fails with the following error:
                             '   Option Strict On disallows implicit conversions
                             '   from 'Boolean?' to 'Boolean'.
    

So, why does this work? Is it a bug (or “hidden feature”) in the compiler or is it a bug in the documentation and Boolean? is actually a valid type for the first argument of If(a, b, c)?

PS: In C#, b ? x : y does not compile if b is of type bool?.


EDIT: I’ve reported this issue to Microsoft Connect. Someone from MS has replied and confirmed that the documentation will be updated to include the Boolean? case.

  • 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-26T01:26:19+00:00Added an answer on May 26, 2026 at 1:26 am

    There’s two “why’s”. Why does it and why did they do it that way. I can answer the first, the second is Microsoft’s.

    If you inspect the code generated from VB.Net using Reflector you’ll see this:

    Dim b As Nullable(Of Boolean) = Nothing
    Dim myString As String = IIf(b.GetValueOrDefault, "True", "False")
    

    Or C#:

    bool? b = null;
    string myString = b.GetValueOrDefault() ? "True" : "False";
    

    So the compiler itself is inserting the GetValueOrDefault for the Nullable(of T)

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

Sidebar

Related Questions

I have the following sample code in a VB.NET console application. It compiles and
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I have the following C# project targetting .NET 4.0 that takes a source code
I've got a C# .NET class library MyClassLibrary that compiles fine. I'm trying to
A followup to Does .NET JIT optimize empty loops away? : The following program
I get the following error when I try to compile an asp.net site using
The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this
This confuses me. The following compiles fine under Eclipse. package com.example.gotchas; public class GenericHelper1
The following code compiles correctly under VC++ 8 on XPSP3, but running it causes

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.