Our Code
  • Andreas Finne (gravatar)

    I thought I'd spread the word about a useful, but not that well-known operator in C#; the null-coalescing operator ??.

    This is what the C# Language Reference has to say about the operator:

    "The null-coalescing operator is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand."

    What this means is that instead of doing:

    if (x == null)
        return -1;
    else
        return x;

    You can do:

    return x ?? -1;
  • Leave a reply

    Name (required)
    Website