using System; using System.Collections.Generic; using System.Linq; namespace mRemoteNG.Tools { public static class Extensions { public static Optional Maybe(this T value) { return new Optional(value); } public static Optional MaybeParse(this T value, Func parseFunc) { try { return new Optional(parseFunc(value)); } catch { return new Optional(); } } /// /// Throws an if the given value is /// null. Otherwise, return the value. /// /// /// /// /// The name of the argument /// public static T ThrowIfNull(this T value, string argName) { if (value == null) throw new ArgumentNullException(argName); return value; } /// /// Throws an if the value /// is null or an empty string. Otherwise, returns the value. /// /// /// public static string ThrowIfNullOrEmpty(this string value, string argName) { if (string.IsNullOrEmpty(value)) throw new ArgumentException("Value cannot be null or empty", argName); return value; } /// /// Perform an action for each item in the given collection. The item /// is the pass along the processing chain. /// /// /// /// /// public static IEnumerable ForEach(this IEnumerable collection, Action action) { collection = collection.ToList(); foreach (var item in collection) action(item); return collection; } } }