+ 1

Strange problem with if-else

I remember this tip in if-else lesson: "When only one line of code is in the if block, the curly braces can be omitted." So let's test this : // Suppose that everything is okay and the condition is true. (Edited) using System; namespace Test { class Program { static void Main(string[] args) { string Value2 = "Hello"; if (true) var Value1 = Value2; // Error : Embedded statement cannot be a declaration or labeled statement } } } So we're not able to omitt curly braces in this situation. Tested in visual studio 2015 Any idea about why such a thing is not possible?

6th Jul 2017, 2:35 PM
D3F4U1T
5 Respuestas
+ 2
What's around that IF statement? What does "something" consist of?
6th Jul 2017, 2:16 PM
AgentSmith
+ 2
@Jamie Thanks for your great answer.
6th Jul 2017, 3:05 PM
D3F4U1T
+ 1
show us the whole code, what is something
6th Jul 2017, 2:11 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
0
OK let me make it clearly. using System; namespace Test { class Program { static void Main(string[] args) { string Value2 = "Hello"; if (true) var Value1 = Value2; // Error: Embedded statement cannot be a declaration or labeled statement. } } }
6th Jul 2017, 2:30 PM
D3F4U1T