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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:19:56+00:00 2026-05-13T09:19:56+00:00

I am extracting a bool value from a (non-generic, heterogeneous) collection. The as operator

  • 0

I am extracting a bool value from a (non-generic, heterogeneous) collection.

The as operator may only be used with reference types, so it is not possible to do use as to try a safe-cast to bool:

// This does not work: "The as operator must be used with a reference type ('bool' is a value type)"
object rawValue = map.GetValue(key);
bool value = rawValue as bool;

Is there something similar that can be done to safely cast an object to a value type without possibility of an InvalidCastException if, for whatever reason, the value is not a boolean?

  • 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-13T09:19:57+00:00Added an answer on May 13, 2026 at 9:19 am

    There are two options… with slightly surprising performance:

    • Redundant checking:

      if (rawValue is bool)
      {
          bool x = (bool) rawValue;
          ...
      }
      
    • Using a nullable type:

      bool? x = rawValue as bool?;
      if (x != null)
      {
          ... // use x.Value
      }
      

    The surprising part is that the performance of the second form is much worse than the first.

    In C# 7, you can use pattern matching for this:

    if (rawValue is bool value)
    {
        // Use value here
    }
    

    Note that you still end up with value in scope (but not definitely assigned) after the if statement.

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

Sidebar

Related Questions

Is there a good library for extracting text from a PDF? I'm willing to
Using C# how should I go about extracting titles subtitles and paragraphs from a
I have an xml file from which I am extracting html using LINQ to
From extracting the values using grep Reading:RG1:+ /user/reading-2/Monday:12 Reading:RG1:- /user/**/Friday:12 Reading:RG1:- /user/**/*.txt:12 Reading:RG1:- /user/tet-23/**/*.txt:12
When extracting data from a list this way line[0:3], line[3][:2], line[3][2:] I receive an
I am having problems extracting a list from a list. (defun delete (a l)
I'm extracting data from a webserivce. This is my select statement to retrieve the
I am extracting emails from Gmail using the following: def getMsgs(): try: conn =
What software is available for extracting metadata from an FLV? I'm testing an flv
I need a good way of extracting values from certain bits inside a unsigned

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.