Browse Source

Added overload of GetValueOrDefault that takes no param.

To match `Nullable<T>`.
pull/3255/head
Steven Kirk 7 years ago
parent
commit
9137cc828a
  1. 20
      src/Avalonia.Base/Data/BindingValue.cs
  2. 20
      src/Avalonia.Base/Data/Optional.cs

20
src/Avalonia.Base/Data/BindingValue.cs

@ -185,6 +185,12 @@ namespace Avalonia.Data
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>
@ -192,6 +198,20 @@ namespace Avalonia.Data
/// <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>

20
src/Avalonia.Base/Data/Optional.cs

@ -65,6 +65,12 @@ namespace Avalonia.Data
/// <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>
@ -72,6 +78,20 @@ namespace Avalonia.Data
/// <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>

Loading…
Cancel
Save