Home > Software design >  Why would you leave the brackets in the code without a condition?
Why would you leave the brackets in the code without a condition?

Time:10-05

Why would you leave the open and close brackets in the code without the condition first? I've seen examples in C# where the code does compile when you have something like this:

{
  Console.WriteLine("Hey");
}

Is this just bad syntax? or something else?

CodePudding user response:

New brackets means new scope. For example if you want to create variable, which should exists only in specific part of code you can surround it with brackets:

{
   var a = 1;
   {
      var b = 2;
      //here 'a' and 'b' exist
   }
   //only 'a' exists
   {
      var b = 2;
      //again 'a' and 'b' exist
   }
   //again only 'a' exists
}
  • Related