Repository: microsoft/kiota (csharp)

Issue:
Invalid C# code generated when API path contains an underscore (_)
### What are you generating using Kiota, clients or plugins?

API Client/SDK

### In what context or format are you using Kiota?

Windows executable

### Client library/SDK language

Csharp

### Describe the bug

When API path contains an underscore, the generated code does not compile. Please see the screenshot with condensed information.

![Image](https://github.com/user-attachments/assets/0f00520f-cfa5-44c4-b62e-fc7c26f4f017)

### Expected behavior

The generated code compiles regardless of quirks in API definitions.

### How to reproduce

Create a minimal API definition that contains an `_` in the path (as exemplified in the screenshot) and try to compile the generated C# client.

### Open API description file

_No response_

### Kiota Version

1.27.0+278d4c17bfbc9fff8d8a1618bd95b43b1e3f7f4d

### Latest Kiota version known to work for scenario above?(Not required)

_No response_

### Known Workarounds

Regex search-replace in visual studio: `Replace all "(^\s+)_ = body", "$1var __D3EFECEF9C3C459FA90E066665D380F2 = body"`

### Configuration

_No response_

### Debug output

_No response_

### Other information

_No response_

Pull request:
Fixes the problem described in #6698 by using explicit `if(ReferenceEquals(p, null)) ...` analogous to strings.
fixes #6698

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a40b84017b..0d27fe1eac 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Changed
 
+- Fixes a bug where invalid C# code is generated when API path contains an underscore [#6698](https://github.com/microsoft/kiota/issues/6698)
+
 ## [1.28.0] - 2025-07-11
 
 ### Added
diff --git a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
index 9c1464323c..7347cc3727 100644
--- a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
+++ b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
@@ -31,7 +31,7 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri
             if (nameof(String).Equals(parameter.Type.Name, StringComparison.OrdinalIgnoreCase) && parameter.Type.CollectionKind == CodeTypeBase.CodeTypeCollectionKind.None)
                 writer.WriteLine($"if(string.IsNullOrEmpty({parameterName})) throw new ArgumentNullException(nameof({parameterName}));");
             else
-                writer.WriteLine($"_ = {parameterName} ?? throw new ArgumentNullException(nameof({parameterName}));");
+                writer.WriteLine($"if(ReferenceEquals({parameterName}, null)) throw new ArgumentNullException(nameof({parameterName}));");
         }
         HandleMethodKind(codeElement, writer, inherits, parentClass, isVoid);
         writer.CloseBlock();


Test patch:
diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs
index 3fed737a38..3db02cd0e6 100644
--- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs
+++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs
@@ -1942,7 +1942,7 @@ public void WritesNullableMethodPrototypeForValueType()
         Assert.Contains("int? sampleParam", result);
         Assert.DoesNotContain("#nullable enable", result);
         Assert.DoesNotContain("#nullable restore", result);
-        Assert.Contains("_ = ra ?? throw new ArgumentNullException(nameof(ra));", result);
+        Assert.Contains("if(ReferenceEquals(ra, null)) throw new ArgumentNullException(nameof(ra));", result);
     }
 
     [Fact]
@@ -2020,7 +2020,7 @@ public void WritesMethodWithEmptyStringAsDefaultValueIfNotNullableAndOptional()
         Assert.DoesNotContain("string? sampleParam = \"\"", result);
         Assert.DoesNotContain("#nullable enable", result);
         Assert.DoesNotContain("#nullable restore", result);
-        Assert.Contains("_ = ra ?? throw new ArgumentNullException(nameof(ra));", result);
+        Assert.Contains("if(ReferenceEquals(ra, null)) throw new ArgumentNullException(nameof(ra));", result);
     }
     [Fact]
     public void WritesDeprecationInformation()