How to Assign an Empty Value When String is Null in C#.NET

In C#, you may come across situations where you need to ensure that a string variable is never null. A common approach is to assign an empty string ("") or string.Empty when the string is null. Here’s a step-by-step tutorial on how to assign an empty value when string is null in C#.net.

When you will try to access a string that contains a null value, then it will through an NullReferenceException exception.

Check the below code:

string myString = null;
Console.WriteLine(myString.Length);

This code will throw a NullReferenceException because you’re trying to access the Length property of null.

Assign an Empty Value When String is Null in C#.NET

The simplest solution is to assign an empty string to the variable if it’s null in C#.Net is like below:

string myString = null;

if (myString == null)
{
    myString = string.Empty;
}

Console.WriteLine(myString.Length);  // Outputs: 0

In this case, if myString is null, it will be assigned an empty string (""), and therefore, myString.Length will return 0 instead of throwing an exception.

c# if null then empty string
c# if null then empty string

Assign an Empty Value When String is Null using Null Coalescing Operator in C#.Net

C# provides a more concise way to achieve the same result using the null coalescing operator (??). Here’s how you can use it to assign an empty value when string in null in C#.net.

string myString = null;

myString = myString ?? string.Empty;

Console.WriteLine(myString.Length);  // Outputs: 0

The ?? operator returns the left-hand operand if it’s not null; otherwise, it returns the right-hand operand. Therefore, if myString is null, string.Empty will be assigned to it.

Using Null-Conditional Operator assign an empty value when the C#.net string in null

Another way to avoid a NullReferenceException when accessing a string property is by using the null-conditional operator (?.). This operator returns null if its left-hand operand is null. Otherwise, it calls the method or accesses the property on the left-hand operand. Here’s an example of how yo use the null-conditional operator.

string myString = null;

int length = myString?.Length ?? 0;

Console.WriteLine(length);  // Outputs: 0

In this case, myString?.Length will return null if myString is null. The ?? operator then assigns 0 to length because the left-hand operand is null.

Using the Null Coalescing Assignment Operator

C# 8.0 introduced the null coalescing assignment operator (??=), which assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. Here’s how you can use it:

string myString = null;

myString ??= string.Empty;

Console.WriteLine(myString.Length);  // Outputs: 0

This code snippet does the same thing as the previous examples: if myString is null, it assigns string.Empty to it.

Conclusion

In this tutorial, you learned how to assign an empty string to a null string variable in C#. You also learned how to use the null coalescing operator (??), the null-conditional operator (?.), and the null coalescing assignment operator (??=) to achieve the same result in a more concise way. Remember to always check for null before accessing a string’s properties or methods to avoid NullReferenceException errors.

You may also like: