 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to capture null reference exception in C#?
It handles errors generated from referencing a null object. The Null reference exception occurs when you are looking to access member fields or function types that points to null.
Let’s say we have the following null string −
string str = null;
Now you try to get the length of the null string, then it would cause an exception −
If(str.Length == null) {} Above the exception will be thrown. Now let us seen how to prevent the null pointer exception to be thrown −
Example
using System; class Program {    static void Main() {       int[] arr = new int[5] {1,2,3,4,5};       display(arr);       arr = null;       display(arr);    }    static void display(int[] arr) {       if (arr == null) {          return;       }       Console.WriteLine(arr.Rank);    } }  Output
1
Advertisements
 