csharp null conditional

Code Example - csharp null conditional

                
                        //Return stirng representation of nullable DateTime
DateTime? x = null;
return x.HasValue == true ? x.Value.ToString() : "No Date";
                    
                
 

null coalescing operator csharp

                        
                                //the null coalescing operator for c# is ??
int? x = null;
int y = 9;
return x ?? y; 
//Will return the value of x if x is not null else return y
                            
                        
 

csharp null accessor

                        
                                int? length = people?.Length; // null if people is null
                            
                        
 

null check syntax csharp

                        
                                //Check if an object is null before calling a function using the ? operator.
//The ? operator is syntactic suger for the if block below:

//Using the ? operator
myObject?.MyFunc();

//Using an if block.
if (myObject != null)
{
  myObject.MyFunc();
}
                            
                        
 

csharp null check

                        
                                if (name is null)
  {
    throw new ArgumentNullException(nameof(name));
  }
                            
                        
 

Null check operator used on a null value

                        
                                if(str != null){
    len = str!.length;
}else{
    len = 0; //return value if str is null
}