How can I write a shorthand of the following scenario?
get { if (_rows == null) { _rows = new List<Row>(); } return _rows; }
The null-coalescing operator ( ?? ) is like a shorthand, inline if/else statement that handles null values. This operator evaluates a reference value and, when found non-null, returns that value. When that reference is null , then the operator returns another, default value instead.
In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.
Using null-coalescing operator ( ?? ):
get { _rows = _rows ?? new List<Row>(); return _rows; }
OR (less readable):
get { return _rows ?? (_rows = new List<Row>()); }
The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With