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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:07:19+00:00 2026-05-23T12:07:19+00:00

Maybe this question has been answered before, but the word if occurs so often

  • 0

Maybe this question has been answered before, but the word if occurs so often it’s hard to find it.

The example doesn’t make sense (the expression is always true), but it illustrates my question.

Why is this code valid:

StringBuilder sb;
if ((sb = new StringBuilder("test")) != null) {
    Console.WriteLine(sb);
}

But this code isn’t:

if ((StringBuilder sb = new StringBuilder("test")) != null) {
    Console.WriteLine(sb);
}

I found a similar question regarding a while statement. The accepted answer there says that in a while statement, it would mean the variable would be defined in each loop. But for my if statement example, that isn’t the case.

So what’s the reason we are not allowed to do this?

  • 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-23T12:07:19+00:00Added an answer on May 23, 2026 at 12:07 pm

    This is because section 8.5.1 of the C# language spec. states:

    Furthermore, a variable initializer in a local variable declaration corresponds exactly to an assignment statement that is inserted immediately after the declaration.

    This basically means that, when you do:

    StringBuilder sb = new StringBuilder("test")
    

    You’re, in effect, doing the exact same thing as:

    StringBuilder sb; sb = new StringBuilder("test")
    

    As such, there is no longer a return value for your check against != null, as the assignment isn’t a single expression, but rather a statement, which is a local-variable-declarator comprised of an identifier followed by an expression.

    The language specification gives this example, stating that this:

    void F() {
       int x = 1, y, z = x * 2;
    }
    

    Is exactly equivalent to:

    void F() {
       int x; x = 1;
       int y;
       int z; z = x * 2;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this has been asked before, but I couldn't find it. My question is
May be this question has been answered before but I couldn't find it. I
Maybe this question has been asked before, but I couldn't find it. I am
Maybe this question has been asked many times before, but I never found a
maybe this question has already been answered somewhere on this site, but I'm not
I know this question has been asked and answered before, but none of the
So sorry if this has been answered before, but I cannot find an answer
I know that maybe this question has been asked before, but I can't seem
Maybe this question has been ask already, but could not find any answer for
I know this sort of question has been banded about before but I've 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.