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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:09:46+00:00 2026-05-16T08:09:46+00:00

While developing ASP.NET applications I often need to parse a boolean value given in

  • 0

While developing ASP.NET applications I often need to parse a boolean value given in string form, e.g. from a query string like ?visible=true

I found two solutions to implement the parsing:

bool Visible
{
    get
    {
        bool b;
        return Boolean.TryParse(this.Request["visible"], out b) && b;
    }
}

or

bool Visible
{
    get
    {
        bool b;
        return Boolean.TryParse(this.Request["visible"], out b) ? b : false;
    }
}

How do you think which way is preferred? And probably faster?

P.S. It’s not a micro-opt, I just want to get know

P.P.S. I’m not familiar with IL so decided to ask here

  • 1 1 Answer
  • 1 View
  • 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-16T08:09:47+00:00Added an answer on May 16, 2026 at 8:09 am

    Don’t micro optimize, make it readable.

    I think this is more readable:

    bool visible;
    Boolean.TryParse(this.Request["visible"], out visible);
    return visible;
    

    readable variable names usually helps 😉 And this implementation actually yields fewer op codes compared to the other two, and I assume it will perform in fewer cycles, being faster than both your attempts.

    So, it’s not only more readable imo, but also faster since it skips the if statement. The other two have equal op codes, just switched the logic around on the checking.

    [Edit – compiled with Release flags – shorter IL]

    If you look at the three following implementations:

    public bool Visible1
    {
        get 
        {
            bool b;
            return Boolean.TryParse(HttpContext.Current.Request["visible"], out b) && b;
        }
    }
    
    public bool Visible2
    {
        get
        {
            bool b;
            return Boolean.TryParse(HttpContext.Current.Request["visible"], out b) ? b : false;
        }
    }
    
    public bool Visible3
    {
        get
        {
            bool b;
            Boolean.TryParse(HttpContext.Current.Request["visible"], out b);
            return b;
        }
    }
    

    will yield the following IL code:

    .method public hidebysig specialname instance bool get_Visible1() cil managed
    {
        .maxstack 2
        .locals init (
        [0] bool b)
        L_0000: call class [System.Web]System.Web.HttpContext [System.Web]System.Web.HttpContext::get_Current()
        L_0005: callvirt instance class [System.Web]System.Web.HttpRequest [System.Web]System.Web.HttpContext::get_Request()
        L_000a: ldstr "visible"
        L_000f: callvirt instance string [System.Web]System.Web.HttpRequest::get_Item(string)
        L_0014: ldloca.s b
        L_0016: call bool [mscorlib]System.Boolean::TryParse(string, bool&)
        L_001b: brfalse.s L_001f
        L_001d: ldloc.0 
        L_001e: ret 
        L_001f: ldc.i4.0 
        L_0020: ret 
    }
    
    .method public hidebysig specialname instance bool get_Visible2() cil managed
    {
        .maxstack 2
        .locals init (
        [0] bool b)
        L_0000: call class [System.Web]System.Web.HttpContext [System.Web]System.Web.HttpContext::get_Current()
        L_0005: callvirt instance class [System.Web]System.Web.HttpRequest [System.Web]System.Web.HttpContext::get_Request()
        L_000a: ldstr "visible"
        L_000f: callvirt instance string [System.Web]System.Web.HttpRequest::get_Item(string)
        L_0014: ldloca.s b
        L_0016: call bool [mscorlib]System.Boolean::TryParse(string, bool&)
        L_001b: brtrue.s L_001f
        L_001d: ldc.i4.0 
        L_001e: ret 
        L_001f: ldloc.0 
        L_0020: ret 
    }
    
    .method public hidebysig specialname instance bool get_Visible3() cil managed
    {
        .maxstack 2
        .locals init (
        [0] bool b)
        L_0000: call class [System.Web]System.Web.HttpContext [System.Web]System.Web.HttpContext::get_Current()
        L_0005: callvirt instance class [System.Web]System.Web.HttpRequest [System.Web]System.Web.HttpContext::get_Request()
        L_000a: ldstr "visible"
        L_000f: callvirt instance string [System.Web]System.Web.HttpRequest::get_Item(string)
        L_0014: ldloca.s b
        L_0016: call bool [mscorlib]System.Boolean::TryParse(string, bool&)
        L_001b: pop 
        L_001c: ldloc.0 
        L_001d: ret 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While developing my ASP.NET MVC, I have started to see the need for a
While developing ASP.net applications on a Windows 7 machine in Visual Studio. Sometimes I
Can I avoid using MS AJAX completely while developing ASP.NET Webforms applications and use
While developing and ASP.NET application in C# or VB using Visual Studio 2005/2008/2010 (Not
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
While developing my app (asp.net mvc3) locally everything was fine using the VS dev
While developing an ASP.NET MVC app, I'm finding a few places where my JsonResult
While developing an ASP.NET application I tried to install IIS Express 7.5 onto Windows
Which IoC to consider while developing ASP.NET Web application project and what are the
While developing an ASP.NET site, is the a way to setup automatic login? For

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.