Skip to content

Generic Extensions

Andrzej Kebab edited this page Jan 23, 2024 · 1 revision

UtilityLibrary.Core

This utility library provides extension methods for generic operations, allowing you to perform various checks and conversions on values.

Null / Empty

IsNull<T>()

Checks if the value is null.

// Example usage with a string
string stringValue = null;
bool isNull = stringValue.IsNull();
Console.WriteLine($"Is null: {isNull}");
// Output: Is null: True

Parameters:

  • value (T): The value to check.

Returns:

  • bool: True if the value is null, false otherwise.

IsNotNull<T>()

Checks if the value is not null.

// Example usage with an integer
int intValue = 42;
bool isNotNull = intValue.IsNotNull();
Console.WriteLine($"Is not null: {isNotNull}");
// Output: Is not null: True

Parameters:

  • value (T): The value to check.

Returns:

  • bool: True if the value is not null, false otherwise.

IsEmpty<T>() where T : struct

Checks if the value is the default value for its type.

// Example usage with a double
double doubleValue = 0.0;
bool isEmpty = doubleValue.IsEmpty();
Console.WriteLine($"Is empty: {isEmpty}");
// Output: Is empty: True

Parameters:

  • value (T): The value to check.

Returns:

  • bool: True if the value is the default value for its type, false otherwise.

IsNotEmpty<T>() where T : struct

Checks if the value is not the default value for its type.

// Example usage with a boolean
bool boolValue = true;
bool isNotEmpty = boolValue.IsNotEmpty();
Console.WriteLine($"Is not empty: {isNotEmpty}");
// Output: Is not empty: True

Parameters:

  • value (T): The value to check.

Returns:

  • bool: True if the value is not the default value for its type, false otherwise.

ToNullable<T>() where T : struct

Converts the value to a nullable type.

// Example usage with an integer
int intValue = 42;
int? nullableInt = intValue.ToNullable();
Console.WriteLine($"Nullable value: {nullableInt}");
// Output: Nullable value: 42

Parameters:

  • value (T): The value to convert.

Returns:

  • T?: The value as a nullable type, or null if the value is the default value for its type.

Equal

EqualsAny<T>(params T[] values)

Checks if the value is equal to any of the provided values.

// Example usage with a string
string stringValue = "apple";
bool equalsAny = stringValue.EqualsAny("orange", "banana", "apple");
Console.WriteLine($"Equals any: {equalsAny}");
// Output: Equals any: True

Parameters:

  • value (T): The value to check.
  • values (T[]): The values to compare to.

Returns:

  • bool: True if the value is equal to any of the provided values, false otherwise.

EqualsNone<T>(params T[] values)

Checks if the value is not equal to any of the provided values.

// Example usage with an integer
int intValue = 42;
bool equalsNone = intValue.EqualsNone(10, 20, 30);
Console.WriteLine($"Equals none: {equalsNone}");
// Output: Equals none: True

Parameters:

  • value (T): The value to check.
  • values (T[]): The values to compare to.

Returns:

  • bool: True if the value is not equal to any of the provided values, false otherwise.

Flag

HasFlags<T>(params T[] flags) where T : struct, IComparable, IFormattable, IConvertible

Checks if the value has all the specified flags.

// Example usage with an enum
[Flags]
enum MyEnum { A = 1, B = 2, C = 4 }

MyEnum value = MyEnum.A | MyEnum.B;
bool hasFlags = value.HasFlags(MyEnum.A, MyEnum.B);
Console.WriteLine($"Has flags: {hasFlags}");
// Output: Has flags: True

Parameters:

  • value (T): The value to check.
  • flags (T[]): The flags to check for.

Returns:

  • bool: True if the value has all the specified flags, false otherwise.

Clone this wiki locally