dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.17k stars 5.83k forks source link

Confusion over embedded statement difference between C/C++ and C# #28064

Open abdekker opened 2 years ago

abdekker commented 2 years ago

[Enter feedback here]

Referring to this article: https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs1023

A suggested resolution to the problem is missing. For example: bool myBool = true; if (myBool) int myInt = 3;

CS1023 can be resolved by adding braces (there may be other solutions): bool myBool = false; if (myBool) { int myInt = 3; }

Optionally, a link to some rational behind why C# is different to C/C++ in this case would be interesting/helpful. In Visual 2019 Pro, targeting C++ 17, the following both work in C++, but both generate CS1023 in C#:

// Works in C++ bool myBool = false; if (myBool) int myInt = 3;

// Also works in C++ bool myBool = false; int i = 0; if (myBool) xx : i++;


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

BillWagner commented 2 years ago

That's a good comment @abdekker

I'll keep this open to consider when we update the compiler warnings and errors section.