,System.Func{--0,System.Boolean},--0)} ,System.Func{--0,System.Boolean},--0,System.Threading.CancellationToken)} )} ,System.Threading.CancellationToken)} ,--0)} ,--0,System.Threading.CancellationToken)}
ObservableAsync.SingleOrDefaultAsync(IObservableAsync, Func, T?) method¶
Defined in
Type: ObservableAsync
Namespace: ReactiveUI.Extensions.Async
Assembly: ReactiveUI.Extensions.dll
Applies to
net10.0, net10.0-browserwasm1.0, net10.0-desktop1.0, net9.0, net9.0-browserwasm1.0, net9.0-desktop1.0, net8.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.2, net8.0-macos14.5, net8.0-tvos17.2, netstandard2.1, net462, net481
Overloads¶
- 1.
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, Func<T, bool> predicate, T? defaultValue) - 2.
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, Func<T, bool> predicate, T? defaultValue, CancellationToken cancellationToken) - 3.
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this) - 4.
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, CancellationToken cancellationToken) - 5.
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, T? defaultValue) - 6.
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, T? defaultValue, CancellationToken cancellationToken)
1. Overload¶
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, Func<T, bool> predicate, T? defaultValue)
Summary: Asynchronously returns the only element of a sequence that satisfies a specified condition, or a default value if no such element exists; this operation throws if more than one matching element is found.
Type parameters
| Name | Description |
|---|---|
T | The type of elements in the source sequence. |
Parameters
| Name | Type | Description |
|---|---|---|
this | [IObservableAsync | The source observable sequence. |
predicate | Func | A function to test each element for a condition. The method returns the element for which this predicate returns true. |
defaultValue | T? | The value to return if no element in the sequence satisfies the condition specified by predicate. |
Returns: ValueTask
Remarks
If more than one element satisfies the condition, an exception is thrown. If no elements satisfy the condition, the specified default value is returned. The operation observes the provided cancellation token.
2. Overload¶
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, Func<T, bool> predicate, T? defaultValue, CancellationToken cancellationToken)
Summary: Asynchronously returns the only element of a sequence that satisfies a specified condition, or a default value if no such element exists; this operation throws if more than one matching element is found.
Type parameters
| Name | Description |
|---|---|
T | The type of elements in the source sequence. |
Parameters
| Name | Type | Description |
|---|---|---|
this | [IObservableAsync | The source observable sequence. |
predicate | Func | A function to test each element for a condition. The method returns the element for which this predicate returns true. |
defaultValue | T? | The value to return if no element in the sequence satisfies the condition specified by predicate. |
cancellationToken | CancellationToken | A cancellation token that can be used to cancel the asynchronous operation. |
Returns: ValueTask
Remarks
If more than one element satisfies the condition, an exception is thrown. If no elements satisfy the condition, the specified default value is returned. The operation observes the provided cancellation token.
3. Overload¶
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this)
Summary: Asynchronously returns the only element of a sequence, or a default value if the sequence is empty; this operation throws an exception if more than one element is found.
Type parameters
| Name | Description |
|---|---|
T | The type of elements in the source sequence. |
Parameters
| Name | Type | Description |
|---|---|---|
this | [IObservableAsync | The source observable sequence. |
Returns: ValueTaskT if the sequence is empty.
4. Overload¶
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, CancellationToken cancellationToken)
Summary: Asynchronously returns the only element of a sequence, or a default value if the sequence is empty; this operation throws an exception if more than one element is found.
Type parameters
| Name | Description |
|---|---|
T | The type of elements in the source sequence. |
Parameters
| Name | Type | Description |
|---|---|---|
this | [IObservableAsync | The source observable sequence. |
cancellationToken | CancellationToken | A cancellation token that can be used to cancel the asynchronous operation. |
Returns: ValueTaskT if the sequence is empty.
5. Overload¶
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, T? defaultValue)
Summary: Asynchronously returns the single element of the sequence, or a specified default value if the sequence is empty. Throws an exception if the sequence contains more than one element.
Type parameters
| Name | Description |
|---|---|
T | The type of elements in the source sequence. |
Parameters
| Name | Type | Description |
|---|---|---|
this | [IObservableAsync | The source observable sequence. |
defaultValue | T? | The value to return if the sequence contains no elements. |
Returns: ValueTask
Remarks
Use this method when you expect the sequence to contain zero or one element. If the sequence contains more than one element, an exception is thrown. If the sequence is empty, the specified default value is returned.
6. Overload¶
public static ValueTask<T?> SingleOrDefaultAsync<T>(this IObservableAsync<T> @this, T? defaultValue, CancellationToken cancellationToken)
Summary: Asynchronously returns the single element of the sequence, or a specified default value if the sequence is empty. Throws an exception if the sequence contains more than one element.
Type parameters
| Name | Description |
|---|---|
T | The type of elements in the source sequence. |
Parameters
| Name | Type | Description |
|---|---|---|
this | [IObservableAsync | The source observable sequence. |
defaultValue | T? | The value to return if the sequence contains no elements. |
cancellationToken | CancellationToken | A cancellation token that can be used to cancel the asynchronous operation. |
Returns: ValueTask
Remarks
Use this method when you expect the sequence to contain zero or one element. If the sequence contains more than one element, an exception is thrown. If the sequence is empty, the specified default value is returned.