Double Question Mark “??” Operator in C# Programming Language
This operator is used in between two variables, and the first variable will be assigned a value depending on the second value’s nullability. The syntax for writing this operator is as follows:
The value of integer “a” will be determined by the nullability of variable “b”, so if it is not null, then integer “a” would be equal to “b”, if variable “b“ is null, then integer “a” would be equal to “c”.
Now we will look into some examples of using the “??” operator in the Ubuntu 20.04 environment.
Example 01: Determining Integer Value With the “??” Operator in Ubuntu 20.04
In this example, we will be discussing how we can check for nullability and assign a value to an integer. The decision-making integer will be assigned with either a null value or some actual value, assigning the resultant integer a value. This will reduce the if and else checks that must be repeated depending on the change, but we will easily determine the result due to the “??” operator.
In this C# program, we will be initiating two integer variables in which one would be a decision-maker while the other would be deterministic upon the decision-making variable. We will use the “??” operator for the integer “y” in which the first operand will be the decision-making variable, and the second operator will be a predefined number. Then we will print the result of the decision, which is also the value of integer “y”, as shown in the output below:
The output clearly shows that the value of integer “y” is 8 and proves that the integer “x” was null.
Example 02: String Variable as the Operand for the Double Question Mark Operator in Ubuntu 20.04
In this example, we will be determining the string variable value by using it as an operand with a double question mark. With the help of a deterministic string variable, the Console.WriteLine() function will decide to print the string variable based on their non-nullability. In this method, we will be using a string in place of an integer to foresee the variation of the null coalescing operator.
In the above C# program, we will be focusing on the string datatype variable to use them as the operands for the null coalescing operator. First, we will initialize the string variables and assign null and non-null values to them. Then we will call the Console.WriteLine() function, and in its parameter brackets, we will use the “??” operator. The right and left operand will be the parameter for the Console.WriteLine() function. After compiling and executing the program, we will get the following output:
Because the integer “n” contained a null value, the first Console.WriteLine() function returned the right operand of the null coalescing operator as a result, while the second Console.WriteLine() function returned the left operand of the null coalescing operator as a result.
Example 03: Using Multiple Null Coalescing Operators in a Pre-defined Method in Ubuntu 20.04
So far, we have used the double question mark operator only once in variables or system-defined methods, so we will use this operator multiple times in the return statement of a method that we will be creating in this example. The return statement will have to face different situations as the parameters of the method will change on each call.
The C# program has the code for a method that decides the return statement using the double question mark operator. First, we’ll build a method called “Problem” that takes two variables as parameters. This procedure will create a variable and assign it a value before writing the return statement, which will sort both parameters and the variable “n3” using the two null coalescing operators. The result of the second null coalescing operator will depend upon the result of the first null coalescing operator. Then in the main program, we will initialize three variables with the method having different parameters in each call. We can see the difference in the result due to the changes in the parameter value by executing the above program.
The output suggests that the first value would be “45” which is also the value of the n2 variable. Then the second value will be “88” which is the value of the n1 variable, and the third value will be “5” which is the value of the n3 variable.
Example 04: Determining String Value With the “??” Operator in Ubuntu 20.04
In this example, we will use a double question mark as an argument to determine the string variable’s value that will be a name. The Console.WriteLine() method will use a deterministic string variable to decide whether or not to output the string variable based on its non-nullability. To account for the null coalescing operator’s variance, we will use a string instead of an integer in this function as the entity to be defined will be a name.
The string datatype variable will be the operands for the null coalescing operator in the aforementioned C# code. We will initialize the string variables n1 and n2 and assign them null and non-null values, respectively. The “??” operator will be used in the “Console.WriteLine()” function’s parameter. The argument for the Console will be the right and left operands of the null coalescing operator. n1 and n2 variables are used in the WriteLine() method. We’ll receive the following result after compiling and running the code:
To begin, we will create a method named “Problem” that takes two parameters. Before writing the return statement, we’ll construct a variable and give it a value, which will sort both parameters and the variable “n3” using the two null coalescing operators.
Example 05: Using the Null Coalescing Operator With Integer and String Variables Simultaneously in Ubuntu 20.04
We will use the null coalescing operator with both integer and string variables in this example. This example will demonstrate the precision that the “??” operator can give and how it can be used to simplify programs that can get more complicated when several checks are used.
In this program, we will be initializing two integer variables and comparing them with each other in the Console.WriteLine() function, to get the non-null integer in the output. Then we will initialize a string variable and assign a null value to it, and in the result string, we will use it as the left operand for the “??” operator and write some text for the right operand. Then after executing the above code, we will get the following output:
We can see in the given output that the n1 integer has a null value, so the right operand, “4”, is written, and the string value is also null; thus, the right operand is displayed.
In this article, we discussed the concept of the double question mark “??” operator in the C# programming language. We discussed the syntax of the null coalescing operator and discussed its working on how it makes a decision. Then we implemented this operator in the Ubuntu 20.04 environment and looked at how it reacts with different functions, data types of variables, and the repeatability factor was also tested. The Double question mark “??” is a good substitute for the traditional If and else statement in the C# programming language.