Skip to content

Object Extensions

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

UtilityLibrary.Core

This utility library provides extension methods for generic and object-related operations, allowing you to perform various checks, type conversions, and formatting on objects.

ObjectExtensions

Null

IsNull()

Checks if the object is null.

// Example usage with an object
object obj = null;
bool isNull = obj.IsNull();
Console.WriteLine($"Is null: {isNull}");
// Output: Is null: True

Parameters:

  • obj (object): The object to check.

Returns:

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

IsNotNull()

Checks if the object is not null.

// Example usage with an object
object obj = new object();
bool isNotNull = obj.IsNotNull();
Console.WriteLine($"Is not null: {isNotNull}");
// Output: Is not null: True

Parameters:

  • obj (object): The object to check.

Returns:

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

Type

CastType<T>()

Casts the object to the specified type.

// Example usage with an object
object obj = "123";
int intValue = obj.CastType<int>();
Console.WriteLine($"Int value: {intValue}");
// Output: Int value: 123

Parameters:

  • obj (object): The object to cast.

Returns:

  • T: The object cast to the specified type, or the default value for the type if the cast fails.

CastType(Type type)

Casts the object to the specified type.

// Example usage with an object
object obj = "123";
Type targetType = typeof(int);
object castedValue = obj.CastType(targetType);
Console.WriteLine($"Casted value: {castedValue}");
// Output: Casted value: 123

Parameters:

  • obj (object): The object to cast.
  • type (Type): The type to cast to.

Returns:

  • object: The object cast to the specified type, or the default value for the type if the cast fails.

ConvertTo<T>(T defaultValue = default(T))

Converts the object to the specified type.

// Example usage with an object
object obj = "123";
int intValue = obj.ConvertTo<int>();
Console.WriteLine($"Int value: {intValue}");
// Output: Int value: 123

Parameters:

  • obj (object): The object to convert.
  • defaultValue (T): The value to return if the conversion fails.

Returns:

  • T: The object converted to the specified type, or the default value if the conversion fails.

CanConvertTo<T>()

Checks if the object can be converted to the specified type.

// Example usage with an object
object obj = "123";
bool canConvert = obj.CanConvertTo<int>();
Console.WriteLine($"Can convert to int: {canConvert}");
// Output: Can convert to int: True

Parameters:

  • obj (object): The object to check.

Returns:

  • bool: True if the object can be converted to the specified type, false otherwise.

As

AsString()

Converts the object to a string.

// Example usage with an object
object obj = 123;
string stringValue = obj.AsString();
Console.WriteLine($"String value: {stringValue}");
// Output: String value: 123

Parameters:

  • obj (object): The object to convert.

Returns:

  • string: The object as a string, or null if the object is null.

AsString(IFormatProvider formatProvider)

Converts the object to a string using the specified format provider.

// Example usage with an object
object obj = 123.456;
string stringValue = obj.AsString(CultureInfo.InvariantCulture);
Console.WriteLine($"String value: {stringValue}");
// Output: String value: 123.456

Parameters:

  • obj (object): The object to convert.
  • formatProvider (IFormatProvider): The format provider to use.

Returns:

  • string: The object as a string using the specified format provider.

AsInvariantString()

Converts the object to a string using the invariant culture.

// Example usage with an object
object obj = 123.456;
string stringValue = obj.AsInvariantString();
Console.WriteLine($"String value: {stringValue}");
// Output: String value: 123.456

Parameters:

  • obj (object): The object to convert.

Returns:

  • string: The object as a string using the invariant culture.

AsInt()

Converts the object to an integer.

// Example usage with an object
object obj = "123";
int intValue = obj.AsInt();
Console.WriteLine($"Int value: {intValue}");
// Output: Int value: 123

Parameters:

  • obj (object): The object to convert.

Returns:

  • int: The object as an integer, or the default value for integers if the conversion fails.

AsLong()

Converts the object to a long.

// Example usage with an object
object obj = "123";
long longValue = obj.AsLong();
Console.WriteLine($"Long value: {longValue}");
// Output: Long value: 123

Parameters:

  • obj (object): The object to convert.

Returns:

  • long: The object as a long, or the default value for longs if the conversion fails.

AsShort()

Converts the object to a short.

// Example usage with an object
object obj = "123";
short shortValue = obj.AsShort();
Console.WriteLine($"Short value: {shortValue}");
// Output: Short value: 123

Parameters:

  • obj (object): The object to convert.

Returns:

  • short: The object as a short, or the default value for shorts if the conversion fails.

AsFloat()

Converts the object to a float.

// Example usage with an object
object obj = "123.456";
float floatValue = obj.AsFloat();
Console.WriteLine($"Float value: {floatValue}");
// Output: Float value: 123.456

Parameters:

  • obj (object): The object to convert.

Returns:

  • float: The object as a float, or the default value for floats if the conversion fails.

AsDouble()

Converts the object to a double.

// Example usage with an

 object
object obj = "123.456";
double doubleValue = obj.AsDouble();
Console.WriteLine($"Double value: {doubleValue}");
// Output: Double value: 123.456

Parameters:

  • obj (object): The object to convert.

Returns:

  • double: The object as a double, or the default value for doubles if the conversion fails.

AsDecimal()

Converts the object to a decimal.

// Example usage with an object
object obj = "123.456";
decimal decimalValue = obj.AsDecimal();
Console.WriteLine($"Decimal value: {decimalValue}");
// Output: Decimal value: 123.456

Parameters:

  • obj (object): The object to convert.

Returns:

  • decimal: The object as a decimal, or the default value for decimals if the conversion fails.

Clone this wiki locally