C#의 Nullable 컴파일러 기능을 사용하면, 마치 TypeScript처럼, 이런 코드를 짤 수 있습니다

// 경고 CS8602: null 가능 참조에 대한 역참조입니다.
Player? player = Game.FindPlayer(0);
player.DoSomething();
// 경고 없음
Player? player = Game.FindPlayer(0);
Assert.IsNotNull(player);
player.DoSomething();

[NotNull] T obj[DoesNotReturnIf(false)] bool condition에 주목해보세요.
#nullable enable
using System.Diagnostics.CodeAnalysis;

static class Assert {
    public static void IsNotNull<T>([NotNull] T obj)
    {
        Verify(obj != null, $"{typeof(T).Name} should not be null.");
    }

    private static void Verify([DoesNotReturnIf(false)] bool condition, string message)
    {
        if (!condition)
        {
            throw new AssertFailedException(message);
        }
    }
}

0

If you have a fediverse account, you can quote this note from your own instance. Search https://mi.rerac.dev/notes/a90lkvphubai023a on your instance and quote it. (Note that quoting is not supported in Mastodon.)