C# null Operator

C# null Operator

In the context of generics, default initializes generic types with their default values.

1 - C# Default Operator

The default value for a reference type is null and the default value of a value type is bitwise zero.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("default(int) is {0}", default(int));//0
            bool b1 = default(int) == null;//False
            Console.WriteLine("default(int) is null ?Answer: {0}", b1);
            Console.WriteLine("default(string) is {0}", default(string));
            //null
            bool b2 = (default(string) == null);//True
            Console.WriteLine("default(string) is  null ? Answer:{0}", b2);
            Console.ReadLine();
        }
    }
}

Output:

C# Default Operator

2 - C# null coalescing Operator

?? operator is the null coalescing operator.

If the operand is non-null, ?? gets its value; otherwise, it returns a default value.

If the left hand expression is non-null, the right hand expression is never evaluated.

string s1 = null;
string s2 = s1 ?? "nothing"; // nothing is assigned to s2

For example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = null;
            string s2 = s1 ?? "XDevSpace is default value";
            Console.WriteLine(s2);
            Console.ReadLine();
        }
    }
}

Output:

C# null coalescing Operator

 

3 - C# null conditional Operator

?. operator is the null-conditional or "Elvis" operator.

?. operator calls methods and access members like the standard dot operator.

If the operand on the left is null, the expression evaluates to null instead of throwing a NullReferenceException:

string sb = null;
string s = sb?.ToString();  // No error; s instead evaluates to null

The last line is equivalent to:

string s = (sb == null ? null : sb.ToString());

null conditional operator short-circuits the remainder of the expression.

string sb = null;
string s = sb?.ToString().ToUpper();// s evaluates to null without error

The following expression deals with situation where both x being null and x.y are null:

x?.y?.z

The final expression must be capable of accepting a null. The following is illegal:

int length = sb?.ToString().Length;   // Illegal : int cannot be null

We can fix this with the use of nullable value types.

int? length = sb?.ToString().Length;   // OK : int? can be null

You can use the null-conditional operator to call a void method:

sb?.SomeVoidMethod();

If sb is null, this becomes a "no-operation" rather than throwing a NullReferenceException.

You can use null conditional Operator with the null coalescing operator:

string s = sb?.ToString() ?? "nothing";   // s evaluates to "nothing"