To check null reference with exception in c#

Jitendrasoft09
Posted by Jitendrasoft09 under C# category on | Points: 40 | Views : 777
using System;

class JkExample {

static void Main(string[] args) {


string myLocalVar = null;

try {


Console.WriteLine("First letter: {0}", myLocalVar[0]);

} catch (NullReferenceException ex) {
Console.WriteLine("Exception: {0}", ex.Message);
}


Console.ReadLine();
}
}

Comments or Responses

Login to post response