C# CS8602 null 許容の警告を解決する
プログラミング
公開: 2023年07月11日
CS8602 - null 参照の可能性があるものの逆参照です。
以下のような構文で expression が not null であることを明示できる。
※コード中に「expression?」という記述があると、以降の expression は nullable と解釈されて警告が表示されるので注意
var expression = helper.CustomFor ?? throw new NullReferenceException(nameof(helper.CustomFor));
メソッドの実装例
public static T GetTagHelper<T>(this ViewDataDictionary viewData) where T : TagHelper
=> viewData[TagHelperKey] as T ?? throw new InvalidOperationException();
var expression = helper.CustomFor