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!"); }
登錄后復(fù)制
上述代碼將允許捕獲異常并使用catch處理它。
以上就是C# 中的空指針異常的詳細(xì)內(nèi)容,更多請關(guān)注www.xfxf.net其它相關(guān)文章!