@@ -41,32 +41,31 @@ public static class ObjectExtensions
4141 private const string ObjectPropertyAssignment = " = " ;
4242
4343 /// <summary>
44- /// Determines whether the current <see cref="IComparable {T}"/> value falls within range, inclusive of the specified minimum and maximum values .
44+ /// Calls the specified <see cref="Action {T}"/> with the current <paramref name="value"/> .
4545 /// </summary>
46- /// <param name="value">The value to test .</param>
47- /// <param name="min ">The inclusive minimum value.</param>
48- /// <param name="max ">The inclusive maximum value.</param >
49- /// <typeparam name="T">The underlying <see cref="IComparable {T}"/> type .</typeparam >
50- /// <returns>
51- /// Returns <see langword="true"/> if the current <see cref="IComparable{T}"/> value falls within range,
52- /// inclusive of the specified minimum and maximum values; otherwise, <see langword="false"/>.
53- /// </returns>
54- public static bool IsWithinRangeInclusive < T > ( this T value , T min , T max ) where T : IComparable < T > =>
55- value . CompareTo ( min ) is 0 or 1 && value . CompareTo ( max ) is 0 or - 1 ;
46+ /// <param name="value">The value to pass to the specified <see cref="Action{T}"/> .</param>
47+ /// <param name="action ">The action into which the current <paramref name=" value"/> will be passed .</param>
48+ /// <typeparam name="T ">The underlying type of the current value.</typeparam >
49+ /// <returns>Returns the current <paramref name="value"/> once the specified <see cref="Action {T}"/> has been executed .</returns >
50+ public static T Apply < T > ( this T value , Action < T > action ) where T : class
51+ {
52+ RequireNotNull ( action , "Action must not be null." , nameof ( action ) ) ;
53+ action ( value ) ;
54+ return value ;
55+ }
5656
5757 /// <summary>
58- /// Determines whether the current <see cref="IComparable{T }"/> value falls within range, exclusive of the specified minimum and maximum values .
58+ /// Calls the specified <see cref="Func{T, TResult }"/> with the current <paramref name="value"/> .
5959 /// </summary>
60- /// <param name="value">The value to test.</param>
61- /// <param name="min">The exclusive minimum value.</param>
62- /// <param name="max">The exclusive maximum value.</param>
63- /// <typeparam name="T">The underlying <see cref="IComparable{T}"/> type.</typeparam>
64- /// <returns>
65- /// Returns <see langword="true"/> if the current <see cref="IComparable{T}"/> value falls within range,
66- /// exclusive of the specified minimum and maximum values; otherwise, <see langword="false"/>.
67- /// </returns>
68- public static bool IsWithinRangeExclusive < T > ( this T value , T min , T max ) where T : IComparable < T > =>
69- value . CompareTo ( min ) is 1 && value . CompareTo ( max ) is - 1 ;
60+ /// <param name="value">The value to pass to the specified <see cref="Func{T, TResult}"/>.</param>
61+ /// <param name="function">The function into which the current <paramref name="value"/> will be passed.</param>
62+ /// <typeparam name="T">The underlying type of the current value.</typeparam>
63+ /// <returns>Returns the result of the specified <see cref="Func{T, TResult}"/>.</returns>
64+ public static T Apply < T > ( this T value , Func < T , T > function ) where T : struct
65+ {
66+ RequireNotNull ( function , "Function must not be null." , nameof ( function ) ) ;
67+ return function ( value ) ;
68+ }
7069
7170 /// <summary>
7271 /// Compares the current <typeparamref name="T"/> instance with the specified <typeparamref name="T"/> instance.
@@ -108,6 +107,48 @@ public static int CompareToNullable<T>(this T left, T? right) where T : struct,
108107 _ => throw new ArgumentException ( $ "Object must be of type { typeof ( T ) . FullName } ", nameof ( right ) )
109108 } ;
110109
110+ /// <summary>
111+ /// Determines whether the current <see cref="IComparable{T}"/> value falls within range, inclusive of the specified minimum and maximum values.
112+ /// </summary>
113+ /// <param name="value">The value to test.</param>
114+ /// <param name="min">The inclusive minimum value.</param>
115+ /// <param name="max">The inclusive maximum value.</param>
116+ /// <typeparam name="T">The underlying <see cref="IComparable{T}"/> type.</typeparam>
117+ /// <returns>
118+ /// Returns <see langword="true"/> if the current <see cref="IComparable{T}"/> value falls within range,
119+ /// inclusive of the specified minimum and maximum values; otherwise, <see langword="false"/>.
120+ /// </returns>
121+ public static bool IsWithinRangeInclusive < T > ( this T value , T min , T max ) where T : IComparable < T > =>
122+ value . CompareTo ( min ) is 0 or 1 && value . CompareTo ( max ) is 0 or - 1 ;
123+
124+ /// <summary>
125+ /// Determines whether the current <see cref="IComparable{T}"/> value falls within range, exclusive of the specified minimum and maximum values.
126+ /// </summary>
127+ /// <param name="value">The value to test.</param>
128+ /// <param name="min">The exclusive minimum value.</param>
129+ /// <param name="max">The exclusive maximum value.</param>
130+ /// <typeparam name="T">The underlying <see cref="IComparable{T}"/> type.</typeparam>
131+ /// <returns>
132+ /// Returns <see langword="true"/> if the current <see cref="IComparable{T}"/> value falls within range,
133+ /// exclusive of the specified minimum and maximum values; otherwise, <see langword="false"/>.
134+ /// </returns>
135+ public static bool IsWithinRangeExclusive < T > ( this T value , T min , T max ) where T : IComparable < T > =>
136+ value . CompareTo ( min ) is 1 && value . CompareTo ( max ) is - 1 ;
137+
138+ /// <summary>
139+ /// Calls the specified <see cref="Func{T, TResult}"/> with the current <paramref name="value"/> as its argument and returns the result.
140+ /// </summary>
141+ /// <param name="value">The value to pass to the specified <see cref="Func{T, TResult}"/>.</param>
142+ /// <param name="function">The function into which the current <paramref name="value"/> will be passed.</param>
143+ /// <typeparam name="TSource">The underlying type of the current value.</typeparam>
144+ /// <typeparam name="TResult">The underlying type of the result value.</typeparam>
145+ /// <returns>Returns the result of the specified <see cref="Func{T, TResult}"/>.</returns>
146+ public static TResult Let < TSource , TResult > ( this TSource value , Func < TSource , TResult > function )
147+ {
148+ RequireNotNull ( function , "Function must not be null." , nameof ( function ) ) ;
149+ return function ( value ) ;
150+ }
151+
111152 /// <summary>
112153 /// Gets a record-like <see cref="String"/> representation of the current <see cref="Object"/> instance.
113154 /// <remarks>This method is designed specifically for record-like objects and may produce undesirable results when applied to primitive-like objects.</remarks>
0 commit comments