Asked  7 Months ago    Answers:  5   Viewed   40 times

Ran across this line of code:

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

What do the two question marks mean, is it some kind of ternary operator? It's hard to look up in Google.



It's the null coalescing operator, and quite like the ternary (immediate-if) operator. See also ?? Operator - MSDN.

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

expands to:

FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();

which further expands to:

if(formsAuth != null)
    FormsAuth = formsAuth;
    FormsAuth = new FormsAuthenticationWrapper();

In English, it means "If whatever is to the left is not null, use that, otherwise use what's to the right."

Note that you can use any number of these in sequence. The following statement will assign the first non-null Answer# to Answer (if all Answers are null then the Answer is null):

string Answer = Answer1 ?? Answer2 ?? Answer3 ?? Answer4;

Also it's worth mentioning while the expansion above is conceptually equivalent, the result of each expression is only evaluated once. This is important if for example an expression is a method call with side effects. (Credit to @Joey for pointing this out.)

Tuesday, June 1, 2021
answered 7 Months ago

I'm tempted to lie and say that English is my second language...but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me.

I looked up the word and I understand it to be a synonym for 'join'.

I'd say a more accurate description of "coalesce" would be "to form one thing from different elements". The "coalescing" of the ?? operator happens because a single value is always resolved from one of the two values. The first non-null value is the result.

Friday, June 25, 2021
answered 6 Months ago

That would be the bitwise left shift operator.

For each shift left, the value is effectively multiplied by 2. So, for example, writing value << 3 will multiply the value by 8.

What it really does internally is move all of the actual bits of the value left one place. So if you have the value 12 (decimal), in binary that is 00001100; shifting it left one place will turn that into 00011000, or 24.

Saturday, July 31, 2021
answered 4 Months ago

## provides a way to concatenate actual arguments during macro expansion.

Monday, August 2, 2021
answered 4 Months ago

You're running into Unity's custom equality operator:

When a MonoBehaviour has fields, in the editor only, we do not set those fields to “real null”, but to a “fake null” object. Our custom == operator is able to check if something is one of these fake null objects, and behaves accordingly. While this is an exotic setup, it allows us to store information in the fake null object that gives you more contextual information when you invoke a method on it, or when you ask the object for a property. Without this trick, you would only get a NullReferenceException, a stack trace, but you would have no idea which GameObject had the MonoBehaviour that had the field that was null.

While running in the editor, Unity replaces your serialized null with a sentinel value that isn't actually null. This allows them to provide more informative error messages in some circumstances.

Is specifiedUISound equal to null? That depends on how you ask. C# has multiple notions of "equality", including data equality and reference equality.

Some checks will say the values are equal: == and Object.Equals

Others will say they are not equal: ?? and Object.ReferenceEquals

This behavior will only occur in the editor. When running in a standalone build, any null values will just be null.

Saturday, November 6, 2021
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :