check if something is null c# code example

Example 1: exception is null c#

//Bug with VS: Exception e == null
//Happens when multiple catch variables are the same
//an example of the solution:
try
{
    // do something
}
catch (WebException webEx) // using a variable named 'webEx' for this catch
{
    Logger.Log("Error while tried to do something. Error: " + webEx.Message); // <-
}
catch (Exception ex) // using a DIFFERENT variable for this one
{
    Logger.Log("Error while tried to do something. Error: " + ex.Message);
}

Example 2: if property is null then c#

The null-conditional operators are short-circuiting.
That is, if one operation in a chain of conditional member or element 
access operations returns null, the rest of the chain doesn't execute. 
In the following example, B is not evaluated if A evaluates to null 
and C is not evaluated if A or B evaluates to null:
CODE
------------------
A?.B?.Do(C);
A?.B?[C];

Example 3: c# check if int is null

// When trying to check if your int is null or i.e. 'not set',
// you will get the following error:

// The result of the expression is always 'false'
// since a value of type 'int' is never equal to 'null' of type 'int?'

// You can however bypass this by converting your int to string:
int myInt = null;
// This Method will return a bool;
bool isNullInt = string.IsNullOrEmpty(myInt.ToString());
// isNullInt will be in this case true