Handling null values : A better approach



Better way to handle null values in c#

Lets say we receive some value and it is null and we are going to use that in our code

var getValue = null;  
var result = getValue / 2;  
Above code will throw a null exception.
 
 
 
To avoid that one might add a condition that could be:
if (getValue != null)  
{  
var result=getValue / 2;  
} 

A better way could be like below: 

var result= (getValue ?? 0) / 2;  

0 Comment's

Comment Form

Submit Comment