NullReferenceException is a C# version of NullPointerException. To handle and catch it in C#, use try-catch.
The below example shows that a variable is set to null and when we try to print it, it throws an exception that gets caught in the catch −
Try { a = null; Console.WriteLine(a); }catch (NullPointerException ex) { Console.WriteLine("Variable is Null!"); }
上述程式碼將允許捕獲異常並使用catch處理它。
以上是C# 中的空指標異常的詳細內容。更多資訊請關注PHP中文網其他相關文章!