Kiota should unconditionally generate C# classes with nullable reference types
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 333
- Avg merge
- 16h 29m
- Merged PRs (30d)
- 116
Description
Currently, Kiota generates C# classes with conditional compilation to use nullable reference types. For example:
```csharp
//
using Microsoft.Kiota.Abstractions.Serialization;
using System.Collections.Generic;
// […snip…]
/// The Notes property
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
public string? Notes { get; set; }
#nullable restore
#else
public string Notes { get; set; }
#endif
/// The subject property
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
public string? Subject { get; set; }
#nullable restore
#else
public string Subject { get; set; }
#endif
```
That's a lot of noise making the generated code hard to read. Instead, it should look like this:
```csharp
//
#nullable enable
using Microsoft.Kiota.Abstractions.Serialization;
using System.Collections.Generic;
// […snip…]
/// The Notes property
public string? Notes { get; set; }
/// The subject property
public string? Subject { get; set; }
```
I know this has been already discussed in https://github.com/microsoft/kiota/issues/2594#issuecomment-1607705061 but I think the conclusion was wrong.
Nullable reference types, [introduced in C# 8](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-80) are a **compiler** feature. So it's totally fine to use nullable reference types even when targeting lower than .NET Standard 2.1, such as .NET Standard 2.0 or .NET Framework 4.6.2, 4.7.x etc.
In order to make the code with nullable reference types compile, one simply has to explicitly specify the C# language version to at least 8.0 in the csproj file:
```xml
netstandard2.0;net462
8.0
```
The requirement for nullable reference types to work is the version of the _SDK_ that is used, not the _target framework_. C# 8 was [introduced along with .NET Core 3.0](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#defaults) meaning that it will work with any SDK from 3.0.0 onwards. And, as stated in [Kiota documentation](https://learn.microsoft.com/en-gb/openapi/kiota/quickstarts/dotnet#required-tools), the .NET 8 SDK is required anyway.
Contributor guide
Assessment
This issue has not been assessed yet.