1

I previously had following code:

 public class Program
    {

        public static void Main(string[] args)
        {
              int i = 0;
           while(condition)
           {
             .........
             .........
            if (condition)           
            {
                i = 6;
                Console.WriteLine("Inside Block :" +i);
            }
           else
            {
            i = 7;
            Console.WriteLine("After block : " + i);
            }
          }
        }               
    }

Later, i figured that i don't need any conditions and want to assign 6 to variable i each time. So i commented out the code but by doing so I made a silly mistake and so now my code looks like following:

public class Program {

        public static void Main(string[] args)
        {
              int i = 0;
           while(condition)
           {
             .........
             .........
           // a block without condition                    
            {
                i = 6;
                Console.WriteLine("Inside Block :" +i);
            }

          }
        }               
    }

As you can see, by mistake i just commented out/deleted the line with if condition but forgot to remove the blocks.Now, what does this block means here. It gets executed every time which i wanted anyway but why it is not giving any error while compiling?

Do we have any terminology/concept here? I know about Initializer Block in Java but that is not what happening here. Will it work differently in Multi-Threading environment?

Any info will be really helpful.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Harshil Doshi
  • 3,497
  • 3
  • 14
  • 37
  • 3
    You're creating a scope. – ProgrammingLlama Feb 13 '19 at 06:59
  • But the variable is declared outside of the block. SO how it will change the scope?And whatever value I am assigning to `i` inside block is also available outside the block. – Harshil Doshi Feb 13 '19 at 07:01
  • 2
    Possible duplicate of [Do you use curly braces for additional scoping?](https://stackoverflow.com/questions/249009/do-you-use-curly-braces-for-additional-scoping) – PSK Feb 13 '19 at 07:02
  • possible duplicate of https://stackoverflow.com/questions/6136853/why-does-c-sharp-allow-code-blocks-without-a-preceding-statement – Arvind Maurya Feb 13 '19 at 07:04
  • @HarshilDoshi that's ok, but u won't be able to access variable if u try to create a local variable in the inner scope – yolo sora Feb 13 '19 at 07:05
  • 1
    It doesn't give you an error or warning for the same reason that if you add superfluous brackets to an expression it doesn't give you an error. It does introduce an additional scope, as others have said, but otherwise, why *should* it care if you're giving it `((((1+4))))` over `1+4`? – Damien_The_Unbeliever Feb 13 '19 at 07:09

2 Answers2

2

why it is not giving any error while compiling?

Do we have any terminology/concept here?

Because C# allows you to do so. C# language specification section 1.5:

A block permits multiple statements to be written in contexts where a single statement is allowed. A block consists of a list of statements written between the delimiters { and }.

So really, the if statements and while statements you are familiar with only need a single statement after their headers, and since a block is permitted wherever a single statement is permitted, you can write a block there.

And these blocks without any headers work like any other block. They create a new scope. Variables declared in the block can't be accessed outside the block.

Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313
2

Basically when you put {} blocks (a.k.a scope) it defines the boundary for code execution.

public static void Main(string[] args)
        {
              int i = 0;
           while(condition)
           {
             .........
             .........
           // a block without condition                    
            {
                i = 6;
                Console.WriteLine("Inside Block :" +i);
            }

          }
        }               
    }

Since your variable i is defined on method level, it doesn't have any significant impact on your code.

Let's say you defined a variable inside that scope, and tried accessing it outside, it would generate an error since any variable defined inside a scope has its limit to the boundaries of scope.

For Example.

public static void Main(string[] args)
        {
              int i = 0;
           while(condition)
           {
             .........
             .........
           // a block without condition                    
            {
                int j = 44;
                i = 6;
                Console.WriteLine("Inside Block :" +i);
            }

          }
          Console.WriteLine(j); // <------------------ This would generate error.
        }               
    }