Table of Contents

Class GeneralExtensions

Namespace
DotNetExtensionKit
Assembly
DotNetExtensionKit.dll

Provides general-purpose utility extension methods including null checks, safe casting, conditional execution, transformations, and reflection helpers.

public static class GeneralExtensions
Inheritance
GeneralExtensions
Inherited Members

Methods

Also<T>(T, Action<T>)

Executes the specified action on the value and returns the original value unchanged.

public static T Also<T>(this T value, Action<T> action)

Parameters

value T

The value to pass to the action.

action Action<T>

The action to execute on the value.

Returns

T

The original value, unchanged.

Type Parameters

T

The type of the value.

Examples

var list = new List<int> { 1, 2, 3 }
    .Also(l => Console.WriteLine(l.Count)); // prints 3, returns the list

As<T>(object?)

Performs a safe cast, returning null if the value is not compatible with T.

public static T? As<T>(this object? value) where T : class

Parameters

value object

The object to cast.

Returns

T

The value cast to T, or null if the cast is not valid.

Type Parameters

T

The target reference type to cast to.

Examples

object obj = "hello";
string? text = obj.As<string>(); // "hello"

CallGenericMethod(object, string, Type[], params object[])

Locates an open generic method by name and arity on the instance's runtime type, closes it with the provided type arguments, invokes it, and returns the result.

public static object CallGenericMethod(this object instance, string methodName, Type[] typeArguments, params object[] parameters)

Parameters

instance object

The object instance on which to invoke the method.

methodName string

The name of the generic method to invoke.

typeArguments Type[]

The type arguments to close the generic method with.

parameters object[]

The parameters to pass to the method.

Returns

object

The return value of the invoked method.

Examples

var result = myInstance.CallGenericMethod(
    "MyMethod", new[] { typeof(string) }, "hello");

Exceptions

InvalidOperationException

Thrown when a method with the specified name and generic arity is not found on the instance's type.

CallGenericMethod(Type, string, Type[], object, params object[])

Locates an open generic method by name and arity on the specified type, closes it with the provided type arguments, invokes it, and returns the result.

public static object CallGenericMethod(this Type type, string methodName, Type[] typeArguments, object instance, params object[] parameters)

Parameters

type Type

The type on which to search for the generic method.

methodName string

The name of the generic method to invoke.

typeArguments Type[]

The type arguments to close the generic method with.

instance object

The object instance on which to invoke the method, or null for static methods.

parameters object[]

The parameters to pass to the method.

Returns

object

The return value of the invoked method.

Examples

var result = typeof(MyClass).CallGenericMethod(
    "MyMethod", new[] { typeof(int) }, myInstance, 42);

Exceptions

InvalidOperationException

Thrown when a method with the specified name and generic arity is not found on the type.

IfNot<T>(T, bool)

Returns the value when the condition is false, or default(T) otherwise.

public static T IfNot<T>(this T value, bool condition)

Parameters

value T

The value to return when the condition is not met.

condition bool

The condition to evaluate.

Returns

T

The original value if condition is false; otherwise, default(T).

Type Parameters

T

The type of the value.

Examples

int score = 100;
int result = score.IfNot(score > 200); // 100

If<T>(T, bool)

Returns the value when the condition is true, or default(T) otherwise.

public static T If<T>(this T value, bool condition)

Parameters

value T

The value to return when the condition is met.

condition bool

The condition to evaluate.

Returns

T

The original value if condition is true; otherwise, default(T).

Type Parameters

T

The type of the value.

Examples

int score = 100;
int result = score.If(score > 50); // 100

IsBetween<T>(T, T, T)

Returns true if the value is within the inclusive range [min, max].

public static bool IsBetween<T>(this T value, T min, T max) where T : IComparable<T>

Parameters

value T

The value to check.

min T

The inclusive lower bound.

max T

The inclusive upper bound.

Returns

bool

true if value is between min and max inclusive; otherwise, false.

Type Parameters

T

The comparable type of the value.

Examples

bool inRange = 5.IsBetween(1, 10); // true

IsIn<T>(T, params T[])

Returns true if the value equals any element in the specified set.

public static bool IsIn<T>(this T value, params T[] values)

Parameters

value T

The value to search for.

values T[]

The set of values to search within.

Returns

bool

true if value is found in values; otherwise, false.

Type Parameters

T

The type of the value and set elements.

Examples

bool found = "b".IsIn("a", "b", "c"); // true

IsNotNull<T>(T?)

Returns true if the value is not null.

public static bool IsNotNull<T>(this T? value) where T : class

Parameters

value T

The value to check for non-null.

Returns

bool

true if value is not null; otherwise, false.

Type Parameters

T

The reference type of the value.

Examples

string name = "Alice";
bool result = name.IsNotNull(); // true

IsNull<T>(T?)

Returns true if the value is null.

public static bool IsNull<T>(this T? value) where T : class

Parameters

value T

The value to check for null.

Returns

bool

true if value is null; otherwise, false.

Type Parameters

T

The reference type of the value.

Examples

string? name = null;
bool result = name.IsNull(); // true

Transform<T, TResult>(T, Func<T, TResult>)

Applies the specified function to the value and returns the result.

public static TResult Transform<T, TResult>(this T value, Func<T, TResult> transformer)

Parameters

value T

The value to transform.

transformer Func<T, TResult>

The function to apply to the value.

Returns

TResult

The result of applying transformer to value.

Type Parameters

T

The type of the input value.

TResult

The type of the result.

Examples

int length = "hello".Transform(s => s.Length); // 5