Beofett
Beofett

Reputation: 2429

Null coalescing operator vs. value()

I'm creating some objects to return to a form via API, and the objects are derived from database values, including values that could be null in the database, but cannot be null in the context of my API (I am obtaining data from multiple tables, so I know that if a field is null in one table, I can obtain a legitimate value from another table):

List<ResultsByLineShiftSlot> returnResults = new List<ResultsByLineShiftSlot>();
            foreach (LineShiftSlot ls in db.LineShiftSlots.OrderBy(ls => ls.ScheduledDateAndTime).Where(ls => ls.ProductionDate == slotDate &&
                ls.ShiftId == shiftId &&
                ls.LineId == lineId &&
                ls.Quantity > 0 &&
                ls.BlendId != null))
            {
                var recordedResult = db.LineShiftSlotResults.FirstOrDefault(r => r.LineShiftSlotId == ls.Id);
                if (recordedResult != null)
                {
                    ResultsByLineShiftSlot returnResult = new ResultsByLineShiftSlot
                    {

                        BlendId = recordedResult.BlendId

                    };
                }
                else
                {
                    ResultsByLineShiftSlot returnResult = new ResultsByLineShiftSlot
                    {

                        BlendId = ls.BlendId ?? 0

                    };

                }
            }
            return returnResults;

In the above example, BlendId can be null in LineShiftSlots, but not in LineShiftSlotResults.

In this context where a nullable variable is known to contain a non-null value, which is better?

Should I use the null coalescing operator:

BlendId = ls.BlendId ?? 0

Or should I use .Value():

BlendId = ls.BlendId.value()

Both compile, and seem to work.

Are they functionally equivalent in this context? Is using one over the other better practice? I know that .value() could potentially return an exception, whereas the null coalescing operator could not, but in this case .value can never be null.

Upvotes: 0

Views: 266

Answers (2)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51420

In the general case, they're not semantically equivalent.

labelId = recordValue.labelId.Value;

This one means I know the value can't be null. Just throw if my assumption is wrong.

On the other hand,

labelId = recordValue.labelId ?? 0;
labelId = recordValue.labelId.GetValueOrDefault();

These two mean that the value may be null, and if that happens, just ignore it, considering it's normal, and substitute it with the default value.

I'd use GetValueOrDefault() in that case, it makes it somewhat more obvious (and the JIT will inline this method).


However, your code is equivalent to this simpler alternative:

labelId = recordValue.labelId ?? otherValue;

Upvotes: 1

Rajdeep Dosanjh
Rajdeep Dosanjh

Reputation: 1187

In this case the code would be the same however have you considered the following

labelId = recordValue.labelId ?? otherValue

which is essentially the following

if(recordValue.labelId != null){
    labelId = recordValue.labelId.Value;
}
else
{
    labelId = otherValue;
}

Upvotes: 0

Related Questions