| | | 1 | | namespace Allyaria.Abstractions.Extensions; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Provides generic extension methods focused on nullable value types, enabling convenient defaults when a value is not |
| | | 5 | | /// present. |
| | | 6 | | /// </summary> |
| | | 7 | | public static class GenericExtensions |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Returns the provided nullable value if it is not <c>null</c>; otherwise, returns the specified default value or |
| | | 11 | | /// <see langword="default" /> for <typeparamref name="T" /> if no default is provided. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <typeparam name="T">A value type.</typeparam> |
| | | 14 | | /// <param name="value">The nullable value to evaluate.</param> |
| | | 15 | | /// <param name="defaultValue"> |
| | | 16 | | /// The value to return when <paramref name="value" /> is <c>null</c>. If omitted, <see langword="default" /> for |
| | | 17 | | /// <typeparamref name="T" /> is used. |
| | | 18 | | /// </param> |
| | | 19 | | /// <returns> |
| | | 20 | | /// The underlying value when <paramref name="value" /> has a value; otherwise, <paramref name="defaultValue" /> if |
| | | 21 | | /// provided, or <see langword="default" /> for <typeparamref name="T" />. |
| | | 22 | | /// </returns> |
| | | 23 | | public static T OrDefault<T>(this T? value, T defaultValue = default) |
| | | 24 | | where T : struct |
| | 92 | 25 | | => value ?? defaultValue; |
| | | 26 | | } |