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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:28:36+00:00 2026-05-28T02:28:36+00:00

Why if I write void Main() { string value = @C:\; if (!string.IsNullOrEmpty(value)) {

  • 0

Why if I write

void Main()
{
      string value = @"C:\";
      if (!string.IsNullOrEmpty(value))  { 
            string sDirectory = Path.GetDirectoryName(value);
      }

}

it compiles.

And in case if I write

void Main()
{
      string value = @"C:\";
      if (!string.IsNullOrEmpty(value))
        string sDirectory = Path.GetDirectoryName(value);


}

It doesn’t ?

It’s clear that from pure functional point of view the declaration of the variable in the second example is useless, but why it magically becomes usefull in first example, so ?

The IL code produced by both examples is exactly the same.

IL_0000:  ldstr       "C:\"
IL_0005:  stloc.0     
IL_0006:  ldloc.0     
IL_0007:  call        System.String.IsNullOrEmpty
IL_000C:  brtrue.s    IL_0015
IL_000E:  ldloc.0     
IL_000F:  call        System.IO.Path.GetDirectoryName

EDIT:

Forgot to mantion that to produce the IL code for the second case (so the case which is not compilable), it’s enough to compile without string sDirectory =

  • 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-28T02:28:37+00:00Added an answer on May 28, 2026 at 2:28 am

    The production for an if statement is in section 8.7.1 of the C# spec, and it goes like this:

    if-statement:
        if   ( boolean-expression )   embedded-statement
        if   ( boolean-expression )   embedded-statement   else   embedded-statement
    

    The start of section 8 of the C# spec explicitly talks about the embedded-statement production after giving the specification for it:

    embedded-statement:
       block
       empty-statement
       expression-statement
       selection-statement
       iteration-statement
       jump-statement
       try-statement
       checked-statement
       unchecked-statement
       lock-statement
       using-statement 
       yield-statement
    

    The embedded-statement nonterminal is used for statements that appear within other statements. The use of embedded-statement rather than statement excludes the use of declaration statements and labeled statements in these contexts. The example

    void F(bool b) {
         if (b)
             int i = 44;
    } 
    

    results in a compile-time error because an if statement requires an embedded-statement rather than a statement for its if branch. If this code were permitted, then the variable i would be declared, but it could never be used. Note, however, that by placing i’s declaration in a block, the example is valid.

    Note that an assignment counts as an expression-statement – but a local variable declaration doesn’t. (That’s a declaration-statement, as in section 8.5.)

    In terms of a design decision, it makes no sense to declare a variable that you can’t then use – so it’s good that the compiler stops you from doing it.

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

Sidebar

Related Questions

How can i do below without assign null value Tex=null? static void Main(string[] args)
For example, static void Main() { var someVar = 3; Console.Write(GetVariableName(someVar)); } The output
using System; using System.Math; class test { public static void Main() { Console.Write(Enter any
I am trying to write to an IBM MQSeries host with: public void WriteMessage(string
The code that I want to write is like this: void MethodOnThreadA() { for
I'm learning java and I have made simple program that simply reads value from
public abstract class Main implements Comparable { public static void main(String[] args) { Integer[]
import java.io.*; import java.util.*; public class Sort { public static void main(String[] args) throws
I have to write program that gets a number n from the user, and
the code: static void Main(string[] args) { Console.WriteLine(Memory mapped file reader started); using (var

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.