| | | 1 | | using System.Collections; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides extension methods for IEnumerable instances, including emptiness checks and printing contents. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class EnumerableExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides enumerable helpers for the given sequence. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="enumerable">The sequence to print.</param> |
| | | 14 | | extension(IEnumerable? enumerable) |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Determines whether the enumerable is null or has no elements. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <returns>True if empty or null; otherwise false.</returns> |
| | | 20 | | public bool IsEmpty() |
| | 6 | 21 | | { |
| | 6 | 22 | | switch (enumerable) |
| | | 23 | | { |
| | | 24 | | case null: |
| | 2 | 25 | | return true; |
| | | 26 | | case ICollection collection: |
| | 0 | 27 | | return collection.Count == 0; |
| | | 28 | | default: |
| | 4 | 29 | | { |
| | 4 | 30 | | var enumerator = enumerable.GetEnumerator(); |
| | | 31 | | try |
| | 4 | 32 | | { |
| | 4 | 33 | | return !enumerator.MoveNext(); |
| | | 34 | | } |
| | | 35 | | finally |
| | 4 | 36 | | { |
| | 4 | 37 | | if (enumerator is IDisposable disposable) |
| | 4 | 38 | | { |
| | 4 | 39 | | disposable.Dispose(); |
| | 4 | 40 | | } |
| | 4 | 41 | | } |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Determines whether the enumerable contains at least one element. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <returns>True if not empty; otherwise false.</returns> |
| | 3 | 50 | | public bool IsNotEmpty() => !IsEmpty(enumerable); |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Prints each item in the enumerable. For complex objects, prints property name and value pairs. |
| | | 54 | | /// </summary> |
| | | 55 | | public void PrintContents() |
| | 3 | 56 | | { |
| | 3 | 57 | | if (enumerable is null) |
| | 1 | 58 | | { |
| | 1 | 59 | | Console.WriteLine("Enumerable is null"); |
| | | 60 | | |
| | 1 | 61 | | return; |
| | | 62 | | } |
| | | 63 | | |
| | 16 | 64 | | foreach (var item in enumerable) |
| | 5 | 65 | | { |
| | 5 | 66 | | if (item == null) |
| | 0 | 67 | | { |
| | 0 | 68 | | Console.WriteLine("null"); |
| | | 69 | | |
| | 0 | 70 | | continue; |
| | | 71 | | } |
| | | 72 | | |
| | 5 | 73 | | var type = item.GetType(); |
| | | 74 | | |
| | 5 | 75 | | if (type.IsPrimitive || item is string || item is decimal) |
| | 3 | 76 | | { |
| | 3 | 77 | | Console.WriteLine(item); |
| | 3 | 78 | | } |
| | | 79 | | else |
| | 2 | 80 | | { |
| | 2 | 81 | | var properties = type.GetProperties(); |
| | 18 | 82 | | foreach (var prop in properties) |
| | 6 | 83 | | { |
| | 6 | 84 | | var value = prop.GetValue(item, null); |
| | 6 | 85 | | Console.WriteLine($"{prop.Name}: {value}"); |
| | 6 | 86 | | } |
| | 2 | 87 | | } |
| | 5 | 88 | | } |
| | 2 | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |