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);
}
}
}#csharp