.NET Fiddle

CSharp · Console

/Program.cs

#nullable enable

using System;

class Program {
  static void Main() {
    string ? nullableString = null; // null 許容参照型を使用した nullableString 変数の宣言

    ProcessString(nullableString); // メソッド呼び出し

    string regularString = "Hello!";
    ProcessString(regularString); // 通常のstringを渡す

    // Nullable Reference Typesの機能を使った場合の例外処理
    string nonNullableString = "This is not nullable";
    nullableString = nonNullableString; // Nullableの変数に非Nullableの値を代入する

    Console.WriteLine(nullableString.ToUpper()); // コンパイルエラーになる可能性があります(Nullableの変数を使用する前にnullチェックが必要)
  }

  static void ProcessString(string ? text) {
    if (text != null) {
      Console.WriteLine(text.ToUpper()); // null チェック後の安全な参照
    } else {
      Console.WriteLine("Input string is null");
    }
  }
}
;