Class UpdateUtils
Provides neat little code-shortcuts for updating properties.
public static class UpdateUtils
- Inheritance
-
objectUpdateUtils
Methods
Swap<T>(ref T, ref T)
Swaps the content of two fields.
public static void Swap<T>(ref T value1, ref T value2)
Parameters
value1
TThe first field which will afterwards carry the content of
value2
.value2
TThe first field which will afterwards carry the content of
value1
.
Type Parameters
T
The type of objects to swap.
To<T>(T, ref T, Action)
Updates a value and calls back a delegate if the original value actually changed.
public static void To<T>(this T value, ref T original, Action updated)
Parameters
value
TThe new value.
original
TThe original value to update.
updated
ActionGets called if value is different from original.
Type Parameters
T
The type of data to update.
To<T>(T, ref T, ref bool)
Updates a value and sets a boolean flag to true
if the original value actually changed.
public static void To<T>(this T value, ref T original, ref bool updated)
Parameters
value
TThe new value.
original
TThe original value to update.
updated
boolGets set to
true
if value is different from original.
Type Parameters
T
The type of data to update.
To<T>(T, ref T, ref bool, ref bool)
Updates a value and sets two boolean flags to true
if the original value actually changed.
public static void To<T>(this T value, ref T original, ref bool updated1, ref bool updated2)
Parameters
value
TThe new value.
original
TThe original value to update.
updated1
boolGets set to
true
if value is different from original.updated2
boolGets set to
true
if value is different from original.
Type Parameters
T
The type of data to update.
To<TIn, TOut>(TIn, Func<TIn, TOut>)
Immediately invokes the specified action
with the value
. Useful for applying null-coalescing operator.
public static TOut To<TIn, TOut>(this TIn value, Func<TIn, TOut> action)
Parameters
value
TInThe value.
action
Func<TIn, TOut>The action to invoke.
Returns
- TOut
Type Parameters
TIn
TOut
Examples
This allows you to write:
Uri? uri = nullableString?.To(x => new Uri(x);
Instead of:
Uri? uri = nullableString == null ? null : new Uri(nullableString);