Sometimes developers get the following exception
“Object reference not set to an instance of an object”. Whenever we got such type of exception our mouth fell open but in C# 6 we can handle it easily using NULL Conditional Operator. So we can handle “System.NullReferenceException” using this operator. I am going to explain it with a very simple example where I am handling NullReferenceException for string datatype.
What is a Null-Conditional operator?
Before C# 6
string name = null;
int length = name.Length;It will throw the following error.

In C# 6.0
using static System.Console;
namespace BNarayan.NullConditionalOperator
{
class Program
{
static void Main(string[] args)
{
string userName = "Banketeshvar Narayan";
userName = null;
if ((userName?.Length??0)>20)
{
WriteLine("UserName can not be more than 20 charcter long.");
}
else
{
WriteLine("UserName length is: {0}", userName?.Length??0);
}
}
}
}Output

If I comment on the line userName = null;
string userName = "Banketeshvar Narayan";
//userName = null;
if ((userName ? .Length ? ? 0) > 20)
{
WriteLine("UserName can not be more than 20 charcter long.");
}
else
{
WriteLine("UserName length is: {0}", userName ? .Length ? ? 0);
}Output

I have tested the output for some statements in the Immediate window which is as follows.

I have explained it in more details.
Must visit the above link it has more explanation.


Banketeshvar NarayanPosted Nov 18, 2015, 5:49 AM EST
Yaduveer Saini Thank you
Yaduveer SainiPosted Nov 18, 2015, 4:27 AM EST
Good One Thanks for Sharing
Banketeshvar NarayanPosted Nov 16, 2015, 5:13 AM EST
Thanks Anish
Banketeshvar NarayanPosted Nov 16, 2015, 5:11 AM EST
Thanks Aishwarya
Aishwarya DPosted Nov 16, 2015, 2:08 AM EST
Thanks for sharing
Anish AnsariPosted Nov 15, 2015, 7:41 PM EST
nice onne
Banketeshvar NarayanPosted Nov 12, 2015, 10:13 PM EST
Thanks & Welcome Suman
Suman VermaPosted Nov 12, 2015, 9:48 PM EST
Thanks for sharing
Banketeshvar NarayanPosted Nov 11, 2015, 3:20 AM EST
Thanks Santhakumar
Santhakumar MunuswamyPosted Nov 11, 2015, 2:09 AM EST
Good one
Banketeshvar NarayanPosted Nov 9, 2015, 8:41 AM EST
Hi all, I have just explained this topic in more details. you can find more explanation here http://forums.asp.net/post/5989234.aspx
Banketeshvar NarayanPosted Nov 8, 2015, 7:50 PM EST
Thanks every one for giving your precious time to read this and appreciate me.
Banketeshvar NarayanPosted Nov 8, 2015, 7:47 PM EST
Thanks Humayun
Humayun Kabir MamunPosted Nov 8, 2015, 7:24 PM EST
Nice...
Banketeshvar NarayanPosted Nov 8, 2015, 6:11 PM EST
thnx Harshad
Harshad PansuriyaPosted Nov 8, 2015, 6:06 PM EST
Nice one
Banketeshvar NarayanPosted Nov 8, 2015, 3:50 AM EST
thanks Rajeesh
Banketeshvar NarayanPosted Nov 8, 2015, 3:50 AM EST
thnx Gowtham
Rajeesh MenothPosted Nov 8, 2015, 3:31 AM EST
Good Start!
Gowtham KTOP 100Posted Nov 8, 2015, 2:00 AM EST
Good One
Banketeshvar NarayanPosted Nov 7, 2015, 6:46 PM EST
Thanks Mukesh
Mukesh KumarPosted Nov 7, 2015, 5:56 PM EST
Good job.....Welcome to tech professional community.