?? operator

Posted on 2012-08-13
Last Modified: 2012-08-13
I've come across this line of code:
WorkItem.State[States.Permit] = myState.Permit ?? null;

Open in new window

The documentation shows:
// y = x, unless x is null, in which case y = -1.
int y = x ?? -1;

Open in new window

so to translate the first statement to y and x:
y = x ?? null;

Open in new window

which is to say, y = x, unless x is null, in which case y = null

Is the ?? necessary here?
Question by:allelopath
    LVL 37

    Assisted Solution

    by:Gerwin Jansen
    From your reasoning, it looks to be not necessary but:

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

    So if mystate.permit is not null, it returns mystate.permit else it returns null (and assigns to workitem.state[states.permit]).
    LVL 5

    Accepted Solution

    Simple answer - No.

    The only thing I can think of is whether the programmer was trying to cover the case when myState is null as well. But that won't work.
    LVL 1

    Author Comment

    So, is there a difference between:
    - assigning x to y when x is null
    - assigning null to y
    LVL 5

    Assisted Solution

    Only in so far as if it is a reference type, having an open reference to myState.permit might stop myState from being garbage collected.

    But I would have thought the fact that it is null would cause the gc to treat them the same.
    LVL 1

    Author Comment

    >> trying to cover the case when myState is null as well. But that won't work.
    Having thought about it for a minute, I would bet that this is indeed the original intent.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now