Skip to content

Commit

Permalink
Added overload of GetValueOrDefault that takes no param.
Browse files Browse the repository at this point in the history
To match `Nullable<T>`.
  • Loading branch information
grokys committed Dec 4, 2019
1 parent 8196e05 commit 9137cc8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Avalonia.Base/Data/BindingValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,33 @@ public BindingValue<T> WithValue(T value)
return new BindingValue<T>(type | BindingValueType.HasValue, value, Error);
}

/// <summary>
/// Gets the value of the binding value if present, otherwise the default value.
/// </summary>
/// <returns>The value.</returns>
public T GetValueOrDefault() => HasValue ? _value : default;

/// <summary>
/// Gets the value of the binding value if present, otherwise a default value.
/// </summary>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value.</returns>
public T GetValueOrDefault(T defaultValue = default) => HasValue ? _value : defaultValue;

/// <summary>
/// Gets the value if present, otherwise the default value.
/// </summary>
/// <returns>
/// The value if present and of the correct type, `default(TResult)` if the value is
/// not present or of an incorrect type.
/// </returns>
public TResult GetValueOrDefault<TResult>()
{
return HasValue ?
_value is TResult result ? result : default
: default;
}

/// <summary>
/// Gets the value of the binding value if present, otherwise a default value.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions src/Avalonia.Base/Data/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,33 @@ public Optional(T value)
/// <inheritdoc/>
public override string ToString() => HasValue ? _value?.ToString() ?? "(null)" : "(empty)";

/// <summary>
/// Gets the value if present, otherwise the default value.
/// </summary>
/// <returns>The value.</returns>
public T GetValueOrDefault() => HasValue ? _value : default;

/// <summary>
/// Gets the value if present, otherwise a default value.
/// </summary>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value.</returns>
public T GetValueOrDefault(T defaultValue = default) => HasValue ? _value : defaultValue;

/// <summary>
/// Gets the value if present, otherwise the default value.
/// </summary>
/// <returns>
/// The value if present and of the correct type, `default(TResult)` if the value is
/// not present or of an incorrect type.
/// </returns>
public TResult GetValueOrDefault<TResult>()
{
return HasValue ?
_value is TResult result ? result : default
: default;
}

/// <summary>
/// Gets the value if present, otherwise a default value.
/// </summary>
Expand Down

0 comments on commit 9137cc8

Please sign in to comment.