|
7 | 7 | using System.Collections.Immutable; |
8 | 8 | using System.Linq; |
9 | 9 | using System.Reflection; |
10 | | -using static BionicCode.Utilities.Net.HelperExtensionsCommon; |
11 | 10 |
|
12 | 11 | /// <summary> |
13 | 12 | /// A collection of extension methods for various default types |
@@ -915,7 +914,7 @@ public static TItem[] MoveRange<TItem>(this TItem[] array, Range range, int newI |
915 | 914 | { |
916 | 915 | ArgumentNullException.ThrowIfNull(predicate, nameof(predicate)); |
917 | 916 |
|
918 | | - return TryFindLast(source, predicate, out TItem result) |
| 917 | + return TryFindLast(source, predicate, out TItem? result) |
919 | 918 | ? result |
920 | 919 | : default; |
921 | 920 | } |
@@ -947,13 +946,13 @@ public static TItem LastInSorted<TItem>(this IEnumerable<TItem> source, Func<TIt |
947 | 946 | ArgumentNullException.ThrowIfNull(predicate, nameof(predicate)); |
948 | 947 |
|
949 | 948 | return source.IsEmpty() |
950 | | - ? throw new InvalidOperationException(ExceptionMessages.InvalidOperationExceptionMessage_CollectionEmpty()) |
951 | | - : TryFindLast(source, predicate, out TItem result) |
952 | | - ? result |
| 949 | + ? throw new InvalidOperationException(ExceptionMessages.InvalidOperationExceptionMessage_CollectionEmpty) |
| 950 | + : TryFindLast(source, predicate, out TItem? result) |
| 951 | + ? result! |
953 | 952 | : throw new InvalidOperationException(ExceptionMessages.GetInvalidOperationExceptionMessage_ItemNotFound(nameof(predicate))); |
954 | 953 | } |
955 | 954 |
|
956 | | - private static bool TryFindLast<TItem>(IEnumerable<TItem> source, Func<TItem, bool> predicate, out TItem result) |
| 955 | + private static bool TryFindLast<TItem>(IEnumerable<TItem> source, Func<TItem, bool> predicate, out TItem? result) |
957 | 956 | { |
958 | 957 | ArgumentNullException.ThrowIfNull(source, nameof(source)); |
959 | 958 | ArgumentNullException.ThrowIfNull(predicate, nameof(predicate)); |
@@ -1128,47 +1127,71 @@ public static string JoinToString<TItem>(this ReadOnlySpan<TItem> source, Func<T |
1128 | 1127 | string result = stringBuilder.ToString(); |
1129 | 1128 | return result; |
1130 | 1129 | } |
1131 | | - |
| 1130 | + |
1132 | 1131 | /// <summary> |
1133 | | - /// Returns the specified collection if it is not <see langword="null"/>; otherwise, creates and returns a new instance of the |
1134 | | - /// collection type. |
| 1132 | + /// Returns the specified value if it is not <see langword="null"/>; otherwise, creates and returns a new instance of the |
| 1133 | + /// value by invoking its parameterless constructor. |
1135 | 1134 | /// </summary> |
1136 | | - /// <remarks>Use this method to ensure that a collection is always available, which helps prevent <see cref="NullReferenceException"/> when working with collections.</remarks> |
1137 | | - /// <typeparam name="TCollection">The type of collection to return. Must be a reference type that implements <see cref="ICollection"/> and has a parameterless |
1138 | | - /// constructor.</typeparam> |
1139 | | - /// <param name="source">The collection to return if it is not <see langword="null"/>; otherwise, a new instance of the specified collection type.</param> |
1140 | | - /// <returns>The original collection if it is not <see langword="null"/>; otherwise, a new instance of the specified collection type.</returns> |
1141 | | - public static TCollection OrNew<TCollection>(this TCollection? source) where TCollection : class, ICollection, new() => source ?? new TCollection(); |
| 1135 | + /// <remarks>Use this method to ensure that an instance is always available, which helps prevent <see cref="NullReferenceException"/> when working with parameters or return values.</remarks> |
| 1136 | + /// <typeparam name="T">The type of instance to validate. Must be a reference or value type that has a parameterless constructor.</typeparam> |
| 1137 | + /// <param name="value">The value to return if it is not <see langword="null"/>; otherwise, a new instance of the specified type.</param> |
| 1138 | + /// <returns>The original value if it is not <see langword="null"/>; otherwise, a new instance of the specified type.</returns> |
| 1139 | + public static T OrNew<T>(this T? value) where T : class, new() => value ?? new T(); |
1142 | 1140 |
|
1143 | 1141 | /// <summary> |
1144 | 1142 | /// Returns the specified collection if it is not <see langword="null"/>; otherwise, returns an empty collection of the same type. |
1145 | 1143 | /// </summary> |
1146 | 1144 | /// <remarks>This method simplifies null checks by ensuring that a collection is never null, which can |
1147 | 1145 | /// help prevent <see cref="NullReferenceException"/> in client code.</remarks> |
1148 | | - /// <typeparam name="TCollection">The type of the collection, which must implement both <see cref="IEmptyCollectionProvider{TCollection}"/> and <see cref="ICollection"/>.</typeparam> |
1149 | | - /// <param name="source">The collection to return if it is not <see langword="null"/>; otherwise, an empty collection of the same type.</param> |
| 1146 | + /// <typeparam name="TCollection">The type of the collection, which must implement both <see cref="IEmptyCollectionProvider{TCollection}"/> and <see cref="IEnumerable"/>.</typeparam> |
| 1147 | + /// <param name="source">The collection to return if it is not <see langword="null"/>.</param> |
1150 | 1148 | /// <returns>The original collection if it is not <see langword="null"/>; otherwise, an empty collection of type <typeparamref name="TCollection"/>.</returns> |
1151 | | - public static TCollection OrEmpty<TCollection>(this TCollection? source) where TCollection : IEmptyCollectionProvider<TCollection>, ICollection => source ?? TCollection.Empty; |
| 1149 | + public static TCollection OrEmpty<TCollection>(this TCollection? source) where TCollection : IEmptyCollectionProvider<TCollection> => source ?? TCollection.Empty; |
1152 | 1150 |
|
1153 | 1151 | /// <summary> |
1154 | 1152 | /// Returns the specified collection if it is not <see langword="null"/>; otherwise, returns an empty collection of the same type by invoking the provided factory <paramref name="emptyCollectionFactory"/>. |
1155 | 1153 | /// </summary> |
1156 | | - /// <remarks>This method simplifies null checks by ensuring that a collection is never null, which can |
| 1154 | + /// <remarks>This method simplifies <see langword="null"/> checks by ensuring that a collection is never <see langword="null"/>, which can |
1157 | 1155 | /// help prevent <see cref="NullReferenceException"/> in client code.</remarks> |
1158 | | - /// <typeparam name="TCollection">The type of the collection, which must implement both <see cref="IEmptyCollectionProvider{TCollection}"/> and <see cref="ICollection"/>.</typeparam> |
1159 | | - /// <param name="source">The collection to return if it is not <see langword="null"/>; otherwise, an empty collection of the same type.</param> |
| 1156 | + /// <typeparam name="TCollection">The type of the collection, which must implement <see cref="IEnumerable"/>.</typeparam> |
| 1157 | + /// <param name="source">The collection to return if it is not <see langword="null"/>.</param> |
1160 | 1158 | /// <param name="emptyCollectionFactory">A factory function to create an empty collection if the source is <see langword="null"/>.</param> |
1161 | 1159 | /// <returns>The original collection if it is not <see langword="null"/>; otherwise, an empty collection of type <typeparamref name="TCollection"/> as the result of invoking the <paramref name="emptyCollectionFactory"/>.</returns> |
1162 | | - public static TCollection OrEmpty<TCollection>(this TCollection? source, Func<TCollection> emptyCollectionFactory) where TCollection : IEmptyCollectionProvider<TCollection>, ICollection |
| 1160 | + public static TCollection OrEmpty<TCollection>(this TCollection? source, Func<TCollection> emptyCollectionFactory) where TCollection : IEnumerable |
1163 | 1161 | { |
1164 | 1162 | ArgumentNullExceptionAdvanced.ThrowIfNull(emptyCollectionFactory); |
1165 | 1163 |
|
1166 | 1164 | return source ?? emptyCollectionFactory.Invoke(); |
1167 | 1165 | } |
1168 | | -} |
1169 | | - public enum AddRangeMode |
1170 | | -{ |
1171 | | - ThrowOnDuplicateKey, |
1172 | | - SkipDuplicateKey, |
| 1166 | + |
| 1167 | + /// <summary> |
| 1168 | + /// Returns the specified array if it is not <see langword="null"/>; otherwise, returns an empty array of the same type. |
| 1169 | + /// </summary> |
| 1170 | + /// <remarks>This method simplifies <see langword="null"/> checks by ensuring that an array is never <see langword="null"/>, which can |
| 1171 | + /// help prevent <see cref="NullReferenceException"/> in client code.</remarks> |
| 1172 | + /// <typeparam name="TItem">The type of the array items.</typeparam> |
| 1173 | + /// <param name="source">The array to return if it is not <see langword="null"/>.</param> |
| 1174 | + /// <returns>The original array if it is not <see langword="null"/>; otherwise, an empty array of type <typeparamref name="TItem"/>.</returns> |
| 1175 | + public static TItem[] OrEmpty<TItem>(this TItem[]? source) => source ?? Array.Empty<TItem>(); |
| 1176 | + |
| 1177 | + /// <summary> |
| 1178 | + /// Returns the specified <see cref="IEnumerable{TItem}"/> if it is not <see langword="null"/>; otherwise, returns an empty <see cref="IEnumerable{TItem}"/>. |
| 1179 | + /// </summary> |
| 1180 | + /// <remarks>This method simplifies <see langword="null"/> checks by ensuring that a <see cref="IEnumerable{TItem}"/> is never <see langword="null"/>, which can |
| 1181 | + /// help prevent <see cref="NullReferenceException"/> in client code.</remarks> |
| 1182 | + /// <typeparam name="TItem">The type of the <see cref="IEnumerable{TItem}"/> items..</typeparam> |
| 1183 | + /// <param name="source">The <see cref="IEnumerable{TItem}"/> to return if it is not <see langword="null"/>.</param> |
| 1184 | + /// <returns>The original <see cref="IEnumerable{TItem}"/> if it is not <see langword="null"/>; otherwise, an empty <see cref="IEnumerable{TItem}"/>.</returns> |
| 1185 | + public static IEnumerable<TItem> OrEmpty<TItem>(this IEnumerable<TItem>? source) => source ?? Enumerable.Empty<TItem>(); |
| 1186 | + |
| 1187 | + /// <summary> |
| 1188 | + /// Returns the specified <see cref="List{TItem}"/> if it is not <see langword="null"/>; otherwise, returns an empty <see cref="List{TItem}"/>. |
| 1189 | + /// </summary> |
| 1190 | + /// <remarks>This method simplifies <see langword="null"/> checks by ensuring that a <see cref="List{TItem}"/> is never <see langword="null"/>, which can |
| 1191 | + /// help prevent <see cref="NullReferenceException"/> in client code.</remarks> |
| 1192 | + /// <typeparam name="TItem">The type of the <see cref="List{TItem}"/> items..</typeparam> |
| 1193 | + /// <param name="source">The <see cref="List{TItem}"/> to return if it is not <see langword="null"/>.</param> |
| 1194 | + /// <returns>The original <see cref="List{TItem}"/> if it is not <see langword="null"/>; otherwise, an empty <see cref="List{TItem}"/>.</returns> |
| 1195 | + public static List<TItem> OrEmpty<TItem>(this List<TItem>? source) => source ?? []; |
1173 | 1196 | } |
1174 | 1197 |
|
0 commit comments