{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The DefaultIfEmpty operator supplies a default element for an empty sequence. One use of this operator is when performing outer joins on two sequences as in the
Outer Join Sample.
EditMethod Signatures
// 1 - Returns the source sequence, or the default(TSource) if the source sequence has no elements
public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
this IEnumerable<TSource> source)
// 2 - Returns the source sequence, or the defaultValue specified if the source sequence has no elements.
public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
this IEnumerable<TSource> source,
TSource defaultValue)EditExceptions
Throws an ArgumentNullException if
source is null
EditPseudo-code
Overload 1
If
source is null, throw an ArgumentNullException.
If there are no elements in the
source sequence, return the result of
default(TSource);
Iterate the
source sequence.
Return the current element (pick up from here when the next element is requested).
Overload 2
If
source is null, throw an ArgumentNullException.
If there are no elements in the
source sequence, return
defaultValue.
Iterate the
source sequence.
Return the current element (pick up from here when the next element is requested).
EditLoop Count
This operator implements the standard
deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.. If there are no elements in the source sequence, one single element is returned using the iterator pattern.
EditCode Samples
TODO:Needs code sample.