codemaid
codemaid copied to clipboard
Incorrect indentation in C#
Environment
- Visual Studio version: Microsoft Visual Studio Enterprise 2019 Version 16.11.16
- CodeMaid version: 12.0
- Code language: C#
Description
Incorrect indentation
Current behavior
for (int i = 0; i < array.Length; i++)
{
if (i == 0)
Console.Write("[" + array[i] + ", ");
else
if (i == array.Length - 1)
Console.Write(array[i] + "]");
else
Console.Write(array[i] + ", ");
}
Expected behavior
for (int i = 0; i < array.Length; i++)
{
if (i == 0)
Console.Write("[" + array[i] + ", ");
else
if (i == array.Length - 1)
_Console.Write(array[i] + "]");
else
Console.Write(array[i] + ", ");_
}
- Desired changes are displayed in italics
Thank you for reporting the issue. Indentation is handled natively by Visual Studio, which CodeMaid invokes as part of its cleanup by default. If you run the Edit->Advanced->Format Document command do you see the same behavior?
I took a look at the code a bit further. I think what's happening is that the first else
and the following if
are effectively an else if
unless you include braces. That's why indentation may not work as you expected.
Yes you are right. Everything works fine with parentheses. Is it possible to extend the possibilities of correct editing without brackets?
I think to do that would mean changing the C# language definition to consider indentation significant, which I do not think Microsoft would be willing to do. I think it would be better to include the braces as the language expects.
OK I understood. Thank you!