Working with VS2022 and .NET 8 with <Nullable>Enabled</Nullable> for my project.
I encounter null pointer exception at run time with the following code:
string[] arrayOfString = new string[2];
Console.WriteLine(String.Join(',', arrayOfString.Select(s => s ?? "null")))
arrayOfString[0].ToString();
The array is created with 2 string where the default value for string which is null.
I fixed the issue with the following initialization:
string?[] arrayOfString = new string[2];
However, I was expecting a warning at compile time (an error at compilation in my case as I set <TreatWarningsAsErrors>True</TreatWarningsAsErrors>) with Nullable enabled?
Is it an expected behavior or a bug?
s => s ?? nullis redundant and can be dropped: "ifsisnull, let it benull"