using System; using System.Linq; using System.Collections.Generic; namespace Chernobyl { /// /// Extension and utility methods for types. /// public static class ObjectExtensions { /// /// Returns a single item as an . For example: /// /// DateTime example = new DateTime(); /// IEnumerable{DateTime} dates = example.ToEnumerable(); /// /// This is used in cases where you need a single object in the form of a collection of objects. /// /// The type that is to be turned into an . /// The item that is to be turned into an . /// The as an public static IEnumerable ToEnumerable(this T item) { yield return item; } /// /// Returns a single item as an if it is not null. If it is null, /// then it returns an empty . For example: /// /// DateTime example = new DateTime(); /// IEnumerable{DateTime} dates = example.ToEnumerableOrEmpty(); /// /// This is used in cases where you need a single object in the form of a collection of objects. /// /// The type that is to be turned into an . /// The item that is to be turned into an . /// The as an public static IEnumerable ToEnumerableOrEmpty(this T item) where T : class => item != null ? item.ToEnumerable() : Enumerable.Empty(); /// /// Invokes with as /// its parameter. /// /// The type of the item to give to the /// . /// The item to give to the . /// The method to invoke with . public static void To(this T item, Action method) => method(item); } }