Home  >  Article  >  Backend Development  >  Null pointer exception in C#

Null pointer exception in C#

PHPz
PHPzforward
2023-09-21 23:57:041480browse

C# 中的空指针异常

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!");
}

The above code will allow to catch the exception and handle it using catch.

The above is the detailed content of Null pointer exception in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete