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); 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> /// <summary>
/// Gets the value of the binding value if present, otherwise a default value. /// Gets the value of the binding value if present, otherwise a default value.
/// </summary> /// </summary>
@ -192,6 +198,20 @@ namespace Avalonia.Data
/// <returns>The value.</returns> /// <returns>The value.</returns>
public T GetValueOrDefault(T defaultValue = default) => HasValue ? _value : defaultValue; 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> /// <summary>
/// Gets the value of the binding value if present, otherwise a default value. /// Gets the value of the binding value if present, otherwise a default value.
/// </summary> /// </summary>

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

@ -65,6 +65,12 @@ namespace Avalonia.Data
/// <inheritdoc/> /// <inheritdoc/>
public override string ToString() => HasValue ? _value?.ToString() ?? "(null)" : "(empty)"; 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> /// <summary>
/// Gets the value if present, otherwise a default value. /// Gets the value if present, otherwise a default value.
/// </summary> /// </summary>
@ -72,6 +78,20 @@ namespace Avalonia.Data
/// <returns>The value.</returns> /// <returns>The value.</returns>
public T GetValueOrDefault(T defaultValue = default) => HasValue ? _value : defaultValue; 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> /// <summary>
/// Gets the value if present, otherwise a default value. /// Gets the value if present, otherwise a default value.
/// </summary> /// </summary>

Loading…
Cancel
Save