dotnet / dotnet/roslyn

C\# Formatter incorrectly indents braces of collection initializers on a clean installation.

Open
#81,083 0 comments 2 reactions 0 assignees View on GitHub
Area-IDE
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/C-Formatter-incorrectly-indents-braces/10973794)._

---
[severity:It bothers me. A fix would be nice]
### \#\# Description

The C\# source code formatter in Visual Studio fails to correctly indent the braces (`{` and `}`) of a standard `Dictionary` collection initializer. The braces are "outdented" by one level, misaligning with the parent declaration.

This bug is reproducible in a completely pristine environment (a new sandbox VM with a default VS installation), proving it is not caused by user settings, extensions, or environmental corruption.

### \#\# Steps to Reproduce

1. Create a new, clean Windows 11 virtual machine (e.g., using Windows Sandbox or a new Azure VM).

2. Install the latest version of Visual Studio Community 2022. **Do not sign in or change any settings.**

3. Launch Visual Studio.

4. Create a new project using the "Console App" (C\#) template.

5. Paste the following code inside the default `Program` class:

```csharp
private static string GetDayIndicatorString(string scheduleCode)
{
// ... (switch statement or other code can go here)

var dayMap = new Dictionary<char, string>
{
{ '0', "Mon" }, { '1', "Tue" }, { '2', "Wed" },
{ '3', "Thu" }, { '4', "Fri" }, { '5', "Sat" }, { '6', "Sun" }
};

return "";
}
```

6. Run the "Format Document" command (press **`Ctrl+K, Ctrl+D`**).

### \#\# Expected Behavior

The formatter should indent the opening and closing braces of the `dayMap` dictionary to align with the `var` keyword, like this:

```csharp
var dayMap = new Dictionary<char, string>
{
{ '0', "Mon" }, { '1', "Tue" }, { '2', "Wed" },
{ '3', "Thu" }, { '4', "Fri" }, { '5', "Sat" }, { '6', "Sun" }
};
```

### \#\# Actual Behavior

The braces are incorrectly outdented by one level, aligning with the parent `private static string` method declaration:

```csharp
var dayMap = new Dictionary<char, string>
{
{ '0', "Mon" }, { '1', "Tue" }, { '2', "Wed" },
{ '3', "Thu" }, { '4', "Fri" }, { '5', "Sat" }, { '6', "Sun" }
};
```

*(Please attach your screenshots here showing the before and after formatting.)*

### \#\# Environment

* **Visual Studio Version:** Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.14.15 (September 2025)
* **.NET SDK Version:** 9.0.305
* **OS:** Windows 11 Pro 24H2

### \#\# Additional Context: Troubleshooting Performed

This issue is not environmental. The following have been systematically tested and **ruled out** as causes:

* **User-specific formatting settings:** The issue occurs with default settings on a fresh installation.
* **Project-specific settings:** No `.editorconfig` file is present.
* **Visual Studio extensions:** The issue is reproducible when launching Visual Studio in `/SafeMode`.
* **Synced user account settings:** The issue is reproducible on a fresh installation **without logging into a Microsoft account**.
* **Corrupted installation:** The issue is reproducible on multiple machines, including a brand-new sandbox VM with a fresh OS and VS install.

---
### Original Comments

#### Feedback Bot on 9/28/2025, 06:46 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

#### YI CHWEEN ONG (Centific Technologies Inc) [MSFT] on 10/1/2025, 06:31 PM:

Thanks for your sharing the detail steps, I can reproduce this issue through your shared steps and this issue has been escalated for further investigation, if there is any process, I will inform you immediately.

#### Feedback Bot on 11/6/2025, 03:42 AM:

This issue is currently being investigated. Our team will get back to you if either more information is needed, a workaround is available, or the issue is resolved.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.