[Fork] [Fork] [Fork] .NET 7 https://stackoverflow.com/questions/66018737/multidimensional-arrays-nullable-reference-types-and-type-conversion

CSharp · Console

/Program.cs

using System;
using System.Reflection;
using System.Linq;
using System.Text.Json;

#nullable enable

//https://stackoverflow.com/questions/66018737/multidimensional-arrays-nullable-reference-types-and-type-conversion
				
//https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/arrays#1721-general
// ... the rank_specifiers are read from left to right before the final non-array element type.
// Example: The type in T[][,,][,] is a single-dimensional array of three-dimensional arrays of two-dimensional arrays of int. end example

//https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references#generics

public static partial class ArrayExtensions
{
	public static T[]?[,] ToArrayOfNullable<T>(this T [,][] a) => a;
	public static T[,]?[] ToArrayOfNullable<T>(this T [][,] a) => a;
}

public class Program
{
	public static void Main()
	{
		Console.WriteLine("Environment version: {0} ({1}), {2}\n", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);

		TestSolution();
	}

	static void TestSolution()
	{
		var nullableArray = (new string?[1,2][]).ToArrayOfNullable();
		nullableArray[0, 0] = null; // No warning
		nullableArray[0, 0] = new string? [] { null }; // No warning
		
		nullableArray.Dump();
	}
}
;