Repository: microsoft/kiota (csharp)

Issue:
Change C# code generation to follow the usual C# code style
### Client library/SDK language

Csharp

### Describe the solution you'd like

There is https://github.com/microsoft/kiota/issues/4347 and https://github.com/microsoft/kiota/pull/4348 but there are still the following cases left:

- Put opening brace after namespace definition on new line <img width="992" alt="image" src="https://github.com/microsoft/kiota/assets/80055480/6637eb29-d7f5-403f-a013-e932af64160e">

- Put opening brace after property definition on new line, if property has getter and setter <img width="1106" alt="image" src="https://github.com/microsoft/kiota/assets/80055480/8bc3dba3-6615-4e55-8f07-9c50bc072dc8">

- Put spaces correctly around dictionary entries <img width="1015" alt="image" src="https://github.com/microsoft/kiota/assets/80055480/804d48b8-6eef-4927-ba0e-747fb2b9aef5">

- Remove trailing space after class definition <img width="1088" alt="image" src="https://github.com/microsoft/kiota/assets/80055480/e4734a7d-5c43-49e5-b445-7aa49149b456">

Can submit a PR.

Pull request description:
Fixes #4625

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 78880149ff..2dc9a45aea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Fixed incorrect optional types in method parameters in Python [#4507](https://github.com/microsoft/kiota/issues/4507)
 - Changed enum parsing methods to return nil in the default case in Go [#4621](https://github.com/microsoft/kiota/issues/4621)
 - Changes the cached description file name to `openapi.yml|json` from `description.yml|json` [#4641](https://github.com/microsoft/kiota/issues/4641)
+- Put opening brace after namespace definition on new line [#4625](https://github.com/microsoft/kiota/issues/4625)
+- Put opening brace after property definition on new line, if property has getter and setter [#4625](https://github.com/microsoft/kiota/issues/4625)
+- Put spaces correctly around dictionary entries [#4625](https://github.com/microsoft/kiota/issues/4625)
+- Remove trailing space after class definition [#4625](https://github.com/microsoft/kiota/issues/4625)
 
 ## [1.14.0] - 2024-05-02
 
diff --git a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs
index 27b7954379..f3431da66f 100644
--- a/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs
+++ b/src/Kiota.Builder/Writers/CSharp/CodeClassDeclarationWriter.cs
@@ -26,7 +26,8 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
                     .OrderBy(static x => x, StringComparer.Ordinal)
                     .ToList()
                     .ForEach(x => writer.WriteLine(x));
-            writer.StartBlock($"namespace {codeElement.Parent.Parent.Name} {{");
+            writer.WriteLine($"namespace {codeElement.Parent.Parent.Name}");
+            writer.StartBlock();
         }
 
         var derivedTypes = (codeElement.Inherits is null ? Enumerable.Empty<string?>() : new string?[] { conventions.GetTypeString(codeElement.Inherits, parentClass) })
@@ -34,7 +35,7 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
                                         .OfType<string>()
                                         .Select(static x => x.ToFirstCharacterUpperCase())
                                         .ToArray();
-        var derivation = derivedTypes.Length != 0 ? ": " + derivedTypes.Aggregate(static (x, y) => $"{x}, {y}") + " " : string.Empty;
+        var derivation = derivedTypes.Length != 0 ? ": " + derivedTypes.Aggregate(static (x, y) => $"{x}, {y}") : string.Empty;
         bool hasDescription = conventions.WriteLongDescription(parentClass, writer);
         conventions.WriteDeprecationAttribute(parentClass, writer);
         if (!hasDescription) writer.WriteLine("#pragma warning disable CS1591");
diff --git a/src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs
index d74a7e6f6f..33a82562ad 100644
--- a/src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs
+++ b/src/Kiota.Builder/Writers/CSharp/CodeEnumWriter.cs
@@ -27,7 +27,8 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
                     .Distinct(StringComparer.Ordinal)
                     .OrderBy(static x => x, StringComparer.Ordinal))
                 writer.WriteLine(x);
-            writer.StartBlock($"namespace {codeNamespace.Name} {{");
+            writer.WriteLine($"namespace {codeNamespace.Name}");
+            writer.StartBlock();
         }
         bool hasDescription = conventions.WriteShortDescription(codeElement, writer);
         if (codeElement.Flags)
diff --git a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
index 852d8b7a12..a34312b563 100644
--- a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
+++ b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
@@ -328,7 +328,7 @@ private void WriteDeserializerBodyForInheritedModel(bool shouldHide, CodeMethod
                                         .Where(static x => !x.ExistsInBaseType)
                                         .OrderBy(static x => x.Name, StringComparer.Ordinal))
         {
-            writer.WriteLine($"{{\"{otherProp.WireName}\", n => {{ {otherProp.Name.ToFirstCharacterUpperCase()} = n.{GetDeserializationMethodName(otherProp.Type, codeElement)}; }} }},");
+            writer.WriteLine($"{{ \"{otherProp.WireName}\", n => {{ {otherProp.Name.ToFirstCharacterUpperCase()} = n.{GetDeserializationMethodName(otherProp.Type, codeElement)}; }} }},");
         }
         writer.CloseBlock("};");
     }
@@ -381,7 +381,7 @@ protected void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams re
             writer.StartBlock();
             foreach (var errorMapping in codeElement.ErrorMappings.Where(errorMapping => errorMapping.Value.AllTypes.FirstOrDefault()?.TypeDefinition is CodeClass))
             {
-                writer.WriteLine($"{{\"{errorMapping.Key.ToUpperInvariant()}\", {conventions.GetTypeString(errorMapping.Value, codeElement, false)}.CreateFromDiscriminatorValue}},");
+                writer.WriteLine($"{{ \"{errorMapping.Key.ToUpperInvariant()}\", {conventions.GetTypeString(errorMapping.Value, codeElement, false)}.CreateFromDiscriminatorValue }},");
             }
             writer.CloseBlock("};");
         }
diff --git a/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs
index 4920ea7360..07d2272db5 100644
--- a/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs
+++ b/src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs
@@ -52,12 +52,11 @@ private void WritePropertyInternal(CodeProperty codeElement, LanguageWriter writ
                 var backingStoreKey = codeElement.WireName;
                 var nullableOp = !codeElement.IsOfKind(CodePropertyKind.AdditionalData) ? "?" : string.Empty;
                 var defaultPropertyValue = codeElement.IsOfKind(CodePropertyKind.AdditionalData) ? " ?? new Dictionary<string, object>()" : string.Empty;
-                writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} {propertyType} {codeElement.Name.ToFirstCharacterUpperCase()} {{");
-                writer.IncreaseIndent();
+                writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} {propertyType} {codeElement.Name.ToFirstCharacterUpperCase()}");
+                writer.StartBlock();
                 writer.WriteLine($"get {{ return {backingStoreProperty.Name.ToFirstCharacterUpperCase()}{nullableOp}.Get<{propertyType}>(\"{backingStoreKey}\"){defaultPropertyValue}; }}");
                 writer.WriteLine($"set {{ {backingStoreProperty.Name.ToFirstCharacterUpperCase()}{nullableOp}.Set(\"{backingStoreKey}\", value); }}");
-                writer.DecreaseIndent();
-                writer.WriteLine("}");
+                writer.CloseBlock();
                 break;
             case CodePropertyKind.ErrorMessageOverride when parentClass.IsErrorDefinition:
                 if (parentClass.GetPrimaryMessageCodePath(static x => x.Name.ToFirstCharacterUpperCase(), static x => x.Name.ToFirstCharacterUpperCase(), "?.") is string primaryMessageCodePath && !string.IsNullOrEmpty(primaryMessageCodePath))


Test patch:
diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs
index f6ebf05944..50a5621c59 100644
--- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs
+++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs
@@ -471,9 +471,9 @@ public void WritesRequestExecutorBody()
         Assert.Contains("var requestInfo", result);
         Assert.Contains("var errorMapping = new Dictionary<string, ParsableFactory<IParsable>>", result);
         Assert.Contains("<exception cref=", result);
-        Assert.Contains("{\"4XX\", Error4XX.CreateFromDiscriminatorValue},", result);
-        Assert.Contains("{\"5XX\", Error5XX.CreateFromDiscriminatorValue},", result);
-        Assert.Contains("{\"401\", Error401.CreateFromDiscriminatorValue},", result);
+        Assert.Contains("{ \"4XX\", Error4XX.CreateFromDiscriminatorValue },", result);
+        Assert.Contains("{ \"5XX\", Error5XX.CreateFromDiscriminatorValue },", result);
+        Assert.Contains("{ \"401\", Error401.CreateFromDiscriminatorValue },", result);
         Assert.Contains("SendAsync", result);
         Assert.Contains($"{ReturnTypeName}.CreateFromDiscriminatorValue", result);
         Assert.Contains(AsyncKeyword, result);
@@ -514,7 +514,7 @@ public void WritesRequestExecutorBodyForCollection()
         var result = tw.ToString();
         Assert.Contains("var requestInfo", result);
         Assert.Contains("var errorMapping = new Dictionary<string, ParsableFactory<IParsable>>", result);
-        Assert.Contains("{\"4XX\", Error4XX.CreateFromDiscriminatorValue},", result);
+        Assert.Contains("{ \"4XX\", Error4XX.CreateFromDiscriminatorValue },", result);
         Assert.Contains("SendCollectionAsync", result);
         Assert.Contains("return collectionResult?.ToList()", result);
         Assert.Contains($"{ReturnTypeName}.CreateFromDiscriminatorValue", result);
@@ -1137,8 +1137,8 @@ public void WritesDeSerializerBody()
         Assert.Contains("GetCollectionOfObjectValues", result);
         Assert.Contains("GetEnumValue", result);
         Assert.Contains("definedInParent", result, StringComparison.OrdinalIgnoreCase);
-        Assert.Contains("{\"DummyUCaseProp", result);
-        Assert.Contains("{\"dummyProp", result);
+        Assert.Contains("{ \"DummyUCaseProp", result);
+        Assert.Contains("{ \"dummyProp", result);
     }
     [Fact]
     public void WritesInheritedSerializerBody()