Question question mark operator

Moment

New member
Joined
Jun 1, 2016
Messages
2
Programming Experience
Beginner
Hello everyone!

I'm a newbie self thought wanna be programmer, I have recently been looking through someone else's c# code I found online, and found something I haven't really seen before could someone explain me what question mark after the type does in this Constructor?


C#:
public Inventory this[int index]
        {
            get
            {
                return _inventories?[index];
            }

            set
            {
                _inventories[index] = value;
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, _inventories[index]));
            }
        }

I know what double exclamation mark (??) does or single exclamation mark with variable declaration e.g. (int?) but the above is something new, I had a quick look around msdn but it doesn't explain much in regard to it.

Thank you for help!
 
This:
return _inventories?[index];
is shorthand for this:
if (_inventories == null)
    return null;
else
    return _inventories[index];
 
This:
return _inventories?[index];
is shorthand for this:
if (_inventories == null)
    return null;
else
    return _inventories[index];

Oh that was simple, thank you !
 
Back
Top Bottom