Table of Contents

Class UpdateUtils

Namespace
NanoByte.Common
Assembly
NanoByte.Common.dll

Provides neat little code-shortcuts for updating properties.

public static class UpdateUtils
Inheritance
object
UpdateUtils

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 T

The first field which will afterwards carry the content of value2.

value2 T

The 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 T

The new value.

original T

The original value to update.

updated Action

Gets 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 T

The new value.

original T

The original value to update.

updated bool

Gets 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 T

The new value.

original T

The original value to update.

updated1 bool

Gets set to true if value is different from original.

updated2 bool

Gets 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 TIn

The 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);