Class ObjectEx
Class containing some extension methods for System.Object.
Inheritance
Inherited Members
Namespace:Extend
Assembly:Extend.dll
Syntax
public static class ObjectEx
Methods
| Improve this Doc View SourceAs<T>(Object)
Cast the given object to the specified type.
Declaration
public static T As<T>(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to cast. |
Returns
Type | Description |
---|---|
T | The object as the specified type. |
Type Parameters
Name | Description |
---|---|
T | The type. |
Chain<T>(T, Action<T>)
Executes the action specified, which the given object as parameter.
Declaration
public static T Chain<T>(this T obj, Action<T> action)
Parameters
Type | Name | Description |
---|---|---|
T | obj | The object to act on. |
System.Action<T><T> | action | The action. |
Returns
Type | Description |
---|---|
T | Returns the given object. |
Type Parameters
Name | Description |
---|---|
T | The type of the object. |
Remarks
Use this method to chain method calls on the same object.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The action can not be null. |
Coalesce<T>(T, T)
Return the first not null value (including value).
Declaration
public static T Coalesce<T>(this T value, T value0)where T : class
Parameters
Type | Name | Description |
---|---|---|
T | value | The first value. |
T | value0 | The second value. |
Returns
Type | Description |
---|---|
T | Returns the first not null value. |
Type Parameters
Name | Description |
---|---|
T | The type of the values. |
Coalesce<T>(T, T[])
Return the first not null value (including value).
Declaration
public static T Coalesce<T>(this T value, params T[] values)where T : class
Parameters
Type | Name | Description |
---|---|---|
T | value | The first value.. |
T[] | values | A list of values. |
Returns
Type | Description |
---|---|
T | Returns the first not null value. |
Type Parameters
Name | Description |
---|---|
T | The type of the values. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The values can not be null. |
System.InvalidOperationException | The array only contains null value. |
CoalesceOrDefault<T>(T, T, T[])
Return the first not null value (including value). If all values are null, returns a default value.
Declaration
public static T CoalesceOrDefault<T>(this T value, T defaultValue, params T[] values)where T : class
Parameters
Type | Name | Description |
---|---|---|
T | value | The first value. |
T | defaultValue | The default value. |
T[] | values | A list of values. |
Returns
Type | Description |
---|---|
T | Returns the first not null value. |
Type Parameters
Name | Description |
---|---|
T | The type of the values. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | values can not be null. |
System.ArgumentNullException | defaultValue can not be null. |
CoalesceOrDefault<T>(T, Func<T>, T[])
Return the first not null value (including value). If all values are null, returns a default value.
Declaration
public static T CoalesceOrDefault<T>(this T value, Func<T> defaultValueFactory, params T[] values)where T : class
Parameters
Type | Name | Description |
---|---|---|
T | value | The first value. |
System.Func<TResult><T> | defaultValueFactory | The default value factory. |
T[] | values | A list of values. |
Returns
Type | Description |
---|---|
T | Returns the first not null value. |
Type Parameters
Name | Description |
---|---|
T | The type of the values. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | values can not be null. |
System.ArgumentNullException | defaultValueFactory can not be null. |
ExecuteSafe<T>(T, Action<T>)
Executes the given action with the value as parameter and handles any exceptions during the execution.
Declaration
public static IExecutionResult<T> ExecuteSafe<T>(this T value, Action<T> action)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value. |
System.Action<T><T> | action | The action. |
Returns
Type | Description |
---|---|
IExecutionResult<T><T> | Returns the given value as result or an exception if one is occurred. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The action can not be null. |
ExecuteSafe<T, TResult>(T, Func<T, TResult>)
Executes the given function with the value as parameter and handles any exceptions during the execution.
Declaration
public static IExecutionResult<TResult> ExecuteSafe<T, TResult>(this T value, Func<T, TResult> func)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value. |
System.Func<T, TResult><T, TResult> | func | The function. |
Returns
Type | Description |
---|---|
IExecutionResult<T><TResult> | Returns the result of the function or an exception if one is occurred. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
TResult | The type of the result. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The func can not be null. |
GetName<TObject, TMember>(TObject, Expression<Func<TObject, TMember>>)
Gets the name of the member to which the given expression points.
Declaration
public static string GetName<TObject, TMember>(this TObject obj, Expression<Func<TObject, TMember>> expression)
Parameters
Type | Name | Description |
---|---|---|
TObject | obj | The source object. |
System.Linq.Expressions.Expression<TDelegate><System.Func<T, TResult><TObject, TMember>> | expression | An expression pointing to the member to get the name of. |
Returns
Type | Description |
---|---|
System.String | Returns the name of the member to which the given expression points. |
Type Parameters
Name | Description |
---|---|
TObject | The type of the source object. |
TMember | The type of the member to which the expression points. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | expression can not be null. |
System.NotSupportedException | The given expression is of a not supported type (supported are: System.Linq.Expressions.ExpressionType.MemberAccess, System.Linq.Expressions.ExpressionType.Convert). |
GetName<TObject, TMember>(TObject, Expression<Func<TMember>>)
Gets the name of the member to which the given expression points.
Declaration
public static string GetName<TObject, TMember>(this TObject obj, Expression<Func<TMember>> expression)
Parameters
Type | Name | Description |
---|---|---|
TObject | obj | The object to call the method on. |
System.Linq.Expressions.Expression<TDelegate><System.Func<TResult><TMember>> | expression | An expression pointing to the member to get the name of. |
Returns
Type | Description |
---|---|
System.String | Returns the name of the member to which the given expression points. |
Type Parameters
Name | Description |
---|---|
TObject | The type of the member to which the expression points. |
TMember | The type of the member to which the expression points. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | expression can not be null. |
System.NotSupportedException | The given expression is of a not supported type (supported are: System.Linq.Expressions.ExpressionType.MemberAccess, System.Linq.Expressions.ExpressionType.Convert). |
GetNameChain<TObject, TMember>(TObject, Expression<Func<TObject, TMember>>)
Gets the name, including a full name chain, of the member to which the given expression points.
Declaration
public static string GetNameChain<TObject, TMember>(this TObject obj, Expression<Func<TObject, TMember>> expression)
Parameters
Type | Name | Description |
---|---|---|
TObject | obj | The object to call the method on. |
System.Linq.Expressions.Expression<TDelegate><System.Func<T, TResult><TObject, TMember>> | expression | An expression pointing to the member to get the name of. |
Returns
Type | Description |
---|---|
System.String | Returns the name, including a full name chain, of the member to which the given expression points. |
Type Parameters
Name | Description |
---|---|
TObject | The type of the member to which the expression points. |
TMember | The type of the member to which the expression points. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | expression can not be null. |
System.NotSupportedException | The given expression is of a not supported type (supported are: System.Linq.Expressions.ExpressionType.MemberAccess, System.Linq.Expressions.ExpressionType.Convert). |
GetNameChain<TObject, TMember>(TObject, Expression<Func<TMember>>)
Gets the name, including a full name chain, of the member to which the given expression points.
Declaration
public static string GetNameChain<TObject, TMember>(this TObject obj, Expression<Func<TMember>> expression)
Parameters
Type | Name | Description |
---|---|---|
TObject | obj | The object to call the method on. |
System.Linq.Expressions.Expression<TDelegate><System.Func<TResult><TMember>> | expression | An expression pointing to the member to get the name of. |
Returns
Type | Description |
---|---|
System.String | Returns the name, including a full name chain, of the member to which the given expression points. |
Type Parameters
Name | Description |
---|---|
TObject | The type of the member to which the expression points. |
TMember | The type of the member to which the expression points. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | expression can not be null. |
System.NotSupportedException | The given expression is of a not supported type (supported are: System.Linq.Expressions.ExpressionType.MemberAccess, System.Linq.Expressions.ExpressionType.Convert). |
IfNull<T>(T, T)
Returns the given value if it is not null, otherwise returns the alternative value.
Declaration
public static T IfNull<T>(this T value, T alternativeValue)where T : class
Parameters
Type | Name | Description |
---|---|---|
T | value | The value. |
T | alternativeValue | The alternative value. |
Returns
Type | Description |
---|---|
T | Returns the value or the alternative value. |
Type Parameters
Name | Description |
---|---|
T | The type of the values. |
IsDefault<T>(T)
Checks if the given value is the default value of it's type.
Declaration
public static bool IsDefault<T>(this T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to check. |
Returns
Type | Description |
---|---|
System.Boolean | Returns true if the value is the default value of it's type. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
IsIn<T>(T, T[])
Checks if the value is present in the given array.
Declaration
public static bool IsIn<T>(this T value, params T[] values)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to search for. |
T[] | values | A IEnumerable containing the values. |
Returns
Type | Description |
---|---|
System.Boolean | >Returns true if the value is present in the array. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The values can not be null. |
IsIn<T>(T, IEnumerable<T>)
Checks if the value is present in the given IEnumerable.
Declaration
public static bool IsIn<T>(this T value, IEnumerable<T> values)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to search for. |
System.Collections.Generic.IEnumerable<T><T> | values | A IEnumerable containing the values. |
Returns
Type | Description |
---|---|
System.Boolean | >Returns true if the value is present in the IEnumerable. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The values can not be null. |
IsNotIn<T>(T, T[])
Checks if the value is not present in the given array.
Declaration
public static bool IsNotIn<T>(this T value, params T[] values)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to search for. |
T[] | values | A IEnumerable containing the values. |
Returns
Type | Description |
---|---|
System.Boolean | >Returns true if the value is not present in the array. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | values can not be null. |
IsNotIn<T>(T, IEnumerable<T>)
Checks if the value is not present in the given IEnumerable.
Declaration
public static bool IsNotIn<T>(this T value, IEnumerable<T> values)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to search for. |
System.Collections.Generic.IEnumerable<T><T> | values | A IEnumerable containing the values. |
Returns
Type | Description |
---|---|
System.Boolean | >Returns true if the value is not present in the IEnumerable. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | values can not be null. |
IsNotNull(Object)
Gets whether the given System.Object is NOT null or not.
Declaration
public static bool IsNotNull(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The System.Object to check. |
Returns
Type | Description |
---|---|
System.Boolean | A value of true if the System.Object is NOT null, otherwise false. |
IsNull(Object)
Gets whether the given System.Object is null or not.
Declaration
public static bool IsNull(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The System.Object to check. |
Returns
Type | Description |
---|---|
System.Boolean | A value of true if the System.Object is null, otherwise false. |
Maximum<TSource>(TSource, TSource[])
Returns the maximum value.
Declaration
public static TSource Maximum<TSource>(this TSource value, params TSource[] values)
Parameters
Type | Name | Description |
---|---|---|
TSource | value | The first value. |
TSource[] | values | The other values. |
Returns
Type | Description |
---|---|
TSource | Returns the maximum value. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the values. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | values can not be null. |
Maximum<TSource, TResult>(TSource, Func<TSource, TResult>, TSource[])
Returns the maximum value.
Declaration
public static TResult Maximum<TSource, TResult>(this TSource value, Func<TSource, TResult> selector, params TSource[] values)
Parameters
Type | Name | Description |
---|---|---|
TSource | value | The first value. |
System.Func<T, TResult><TSource, TResult> | selector | A transform function to apply to each element. |
TSource[] | values | The other values. |
Returns
Type | Description |
---|---|
TResult | Returns the maximum value. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the values. |
TResult | The type of the result. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | values can not be null. |
System.ArgumentNullException | selector can not be null. |
Minimum<TSource>(TSource, TSource[])
Returns the minimum value.
Declaration
public static TSource Minimum<TSource>(this TSource value, params TSource[] values)
Parameters
Type | Name | Description |
---|---|---|
TSource | value | The first value. |
TSource[] | values | The other values. |
Returns
Type | Description |
---|---|
TSource | Returns the minimum value. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the values. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | values can not be null. |
Minimum<TSource, TResult>(TSource, Func<TSource, TResult>, TSource[])
Returns the minimum value.
Declaration
public static TResult Minimum<TSource, TResult>(this TSource value, Func<TSource, TResult> selector, params TSource[] values)
Parameters
Type | Name | Description |
---|---|---|
TSource | value | The first value. |
System.Func<T, TResult><TSource, TResult> | selector | A transform function to apply to each element. |
TSource[] | values | The other values. |
Returns
Type | Description |
---|---|
TResult | Returns the minimum value. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the values. |
TResult | The type of the result. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | values can not be null. |
System.ArgumentNullException | selector can not be null. |
RefEquals(Object, Object)
Determines whether the specified objects instances are the same instance.
Declaration
public static bool RefEquals(this object obj0, object obj1)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj0 | The first object to compare. |
System.Object | obj1 | The second object to compare. |
Returns
Type | Description |
---|---|
System.Boolean | Returns true if the objects are the same instance. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The first object can not be null. |
System.ArgumentNullException | The second object can not be null. |
Satisfies<T>(T, ISpecification<T>)
Checks if the objects satisfies the given specification.
Declaration
public static bool Satisfies<T>(this T obj, ISpecification<T> specification)
Parameters
Type | Name | Description |
---|---|---|
T | obj | The object to check. |
ISpecification<T><T> | specification | The specification to use. |
Returns
Type | Description |
---|---|
System.Boolean | Returns true if the object satisfies the specification; otherwise, false. |
Type Parameters
Name | Description |
---|---|
T | The type of the object. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | specification can not be null. |
SatisfiesWithMessages<T>(T, ISpecification<T>)
Checks if the objects satisfies the given specification.
Declaration
public static IEnumerable<string> SatisfiesWithMessages<T>(this T obj, ISpecification<T> specification)
Parameters
Type | Name | Description |
---|---|---|
T | obj | The object to check. |
ISpecification<T><T> | specification | The specification to use. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T><System.String> |
Type Parameters
Name | Description |
---|---|
T | The type of the object. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | specification can not be null. |
Specification<T>(T, Func<T, Boolean>, String)
Creates a specification with the given condition and message.
Declaration
public static ISpecification<T> Specification<T>(this T obj, Func<T, bool> expression, string message = null)
Parameters
Type | Name | Description |
---|---|---|
T | obj | The object used to create the expression. (Can be null) |
System.Func<T, TResult><T, System.Boolean> | expression | An expression determining whether an object matches the specification or not. |
System.String | message | An error messaged, returned when an object doesn't match the specification. |
Returns
Type | Description |
---|---|
ISpecification<T><T> | Returns a specification with the given condition and message. |
Type Parameters
Name | Description |
---|---|
T | The target type of the expression. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | expression can not be null. |
Swap<T>(Object, ref T, ref T)
Swaps the given values.
Declaration
public static void Swap<T>(this object obj, ref T value0, ref T value1)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object to cal the extension method on, can be null. |
T | value0 | The first value. |
T | value1 | The second value. |
Type Parameters
Name | Description |
---|---|
T | The type of the values. |
ThrowIfNull<TObject>(TObject, Expression<Func<TObject>>, String)
Throws a System.ArgumentNullException exception if obj is null.
Declaration
[Obsolete("This method will be removed in future releases. Please use the overload accepting the argument name as string together with the new nameof keyword (value.ThrowIfNull( nameof( value ) );)")]
public static void ThrowIfNull<TObject>(this TObject obj, Expression<Func<TObject>> expression, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
TObject | obj | The object to check. |
System.Linq.Expressions.Expression<TDelegate><System.Func<TResult><TObject>> | expression | An expression pointing to obj. |
System.String | errorMessage | The text used as exception message if obj is null. |
Type Parameters
Name | Description |
---|---|
TObject | The type obj. |
Remarks
If errorMessage is null, this method will use the following default message: "{object name} can not be null."
ThrowIfNull<TObject>(TObject, String, String)
Throws a System.ArgumentNullException exception if obj is null.
Declaration
public static void ThrowIfNull<TObject>(this TObject obj, string parameterName, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
TObject | obj | The object to check. |
System.String | parameterName | The name of obj. |
System.String | errorMessage | The text used as exception message if obj is null. |
Type Parameters
Name | Description |
---|---|
TObject | The type obj. |
Remarks
If errorMessage is null, this method will use the following default message: "{object name} can not be null."
ToBoolean(Object)
Converts the value of a specified object to an equivalent Boolean value.
Declaration
public static bool ToBoolean(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null.. |
Returns
Type | Description |
---|---|
System.Boolean | true or false, which reflects the value returned by invoking the System.IConvertible.ToBoolean(System.IFormatProvider) method for the underlying type of value. If value is null, the method returns false. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is a string that does not equal System.Boolean.TrueString or System.Boolean.FalseString. |
System.InvalidCastException | value does not implement the IConvertible interface. The conversion of value to a System.Boolean is not supported. |
ToBoolean(Object, IFormatProvider)
Converts the value of the specified object to an equivalent Boolean value, using the specified culture-specific formatting information.
Declaration
public static bool ToBoolean(this object obj, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
System.IFormatProvider | formatProvider | The format provider. |
Returns
Type | Description |
---|---|
System.Boolean | true or false, which reflects the value returned by invoking the System.IConvertible.ToBoolean(System.IFormatProvider) method for the underlying type of value. If value is null, the method returns false. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is a string that does not equal System.Boolean.TrueString or System.Boolean.FalseString. |
System.InvalidCastException | value does not implement the IConvertible interface. The conversion of value to a System.Boolean is not supported. |
ToByte(Object)
Converts the value of the specified object to an 8-bit unsigned integer.
Declaration
public static byte ToByte(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface. |
Returns
Type | Description |
---|---|
System.Byte | An 8-bit unsigned integer that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in the property format for a System.Byte value. |
System.InvalidCastException | value does not implement System.IConvertible. -or-Conversion from value to the System.Byte type is not supported. |
System.OverflowException | value represents a number that is less than System.Byte.MinValue or greater than System.Byte.MaxValue. |
ToByte(Object, IFormatProvider)
Converts the value of the specified object to an 8-bit unsigned integer, using the specified culture-specific formatting information.
Declaration
public static byte ToByte(this object obj, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface. |
System.IFormatProvider | formatProvider | The format provider. |
Returns
Type | Description |
---|---|
System.Byte | An 8-bit unsigned integer that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in the property format for a System.Byte value. |
System.InvalidCastException | value does not implement System.IConvertible. -or-Conversion from value to the System.Byte type is not supported. |
System.OverflowException | value represents a number that is less than System.Byte.MinValue or greater than System.Byte.MaxValue. |
ToChar(Object)
Converts the value of the specified object to a Unicode character.
Declaration
public static char ToChar(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface. |
Returns
Type | Description |
---|---|
System.Char | A Unicode character that is equivalent to value, or System.Char.MinValue if value is null. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | value is a null System.String. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion of value to a System.Char is not supported. |
System.OverflowException | value is less than System.Char.MinValue or greater than System.Char.MaxValue. |
ToChar(Object, IFormatProvider)
Converts the value of the specified object to its equivalent Unicode character, using the specified culture-specific formatting information.
Declaration
public static char ToChar(this object obj, IFormatProvider provider)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface. |
System.IFormatProvider | provider | A format provider. |
Returns
Type | Description |
---|---|
System.Char | A Unicode character that is equivalent to value, or System.Char.MinValue if value is null. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | value is a null System.String. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion of value to a System.Char is not supported. |
System.OverflowException | value is less than System.Char.MinValue or greater than System.Char.MaxValue. |
ToDateTime(Object)
Converts the value of the specified object to a System.DateTime object.
Declaration
public static DateTime ToDateTime(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
Returns
Type | Description |
---|---|
System.DateTime | The date and time equivalent of the value of value, or a date and time equivalent of System.DateTime.MinValue if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not a valid date and time value. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
ToDateTime(Object, IFormatProvider)
Converts the value of the specified object to a System.DateTime object, using the specified culture-specific formatting information.
Declaration
public static DateTime ToDateTime(this object obj, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
System.IFormatProvider | formatProvider | The format provider. |
Returns
Type | Description |
---|---|
System.DateTime | The date and time equivalent of the value of value, or a date and time equivalent of System.DateTime.MinValue if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not a valid date and time value. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
ToDecimal(Object)
Converts the value of the specified object to an equivalent decimal number.
Declaration
public static decimal ToDecimal(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
Returns
Type | Description |
---|---|
System.Decimal | A decimal number that is equivalent to value, or 0 (zero) if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for a System.Decimal type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. -or-The conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Decimal.MinValue or greater than System.Decimal.MaxValue. |
ToDecimal(Object, IFormatProvider)
Converts the value of the specified object to an equivalent decimal number, using the specified culture-specific formatting information.
Declaration
public static decimal ToDecimal(this object obj, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
System.IFormatProvider | formatProvider | The format provider. |
Returns
Type | Description |
---|---|
System.Decimal | A decimal number that is equivalent to value, or 0 (zero) if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for a System.Decimal type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. -or-The conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Decimal.MinValue or greater than System.Decimal.MaxValue. |
ToDouble(Object)
Converts the value of the specified object to a double-precision floating-point number.
Declaration
public static double ToDouble(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
Returns
Type | Description |
---|---|
System.Double | A double-precision floating-point number that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for a System.Double type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Double.MinValue or greater than System.Double.MaxValue. |
ToDouble(Object, IFormatProvider)
Converts the value of the specified object to an double-precision floating-point number, using the specified culture-specific formatting information.
Declaration
public static double ToDouble(this object obj, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
System.IFormatProvider | formatProvider | The format provider. |
Returns
Type | Description |
---|---|
System.Double | A double-precision floating-point number that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for a System.Double type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Double.MinValue or greater than System.Double.MaxValue. |
ToInt16(Object)
Converts the value of the specified object to a 16-bit signed integer.
Declaration
public static short ToInt16(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
Returns
Type | Description |
---|---|
System.Int16 | A 16-bit signed integer that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for an System.Int16 type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Int16.MinValue or greater than System.Int16.MaxValue. |
ToInt16(Object, IFormatProvider)
Converts the specified string representation of a number to an equivalent 16-bit signed integer, using the specified culture-specific formatting information.
Declaration
public static short ToInt16(this object obj, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
System.IFormatProvider | formatProvider | The format provider. |
Returns
Type | Description |
---|---|
System.Int16 | A 16-bit signed integer that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for an System.Int16 type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Int16.MinValue or greater than System.Int16.MaxValue. |
ToInt32(Object)
Converts the value of the specified object to a 32-bit signed integer.
Declaration
public static int ToInt32(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
Returns
Type | Description |
---|---|
System.Int32 | A 32-bit signed integer that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for an System.Int32 type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Int32.MinValue or greater than System.Int32.MaxValue. |
ToInt32(Object, IFormatProvider)
Converts the specified string representation of a number to an equivalent 32-bit signed integer, using the specified culture-specific formatting information.
Declaration
public static int ToInt32(this object obj, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
System.IFormatProvider | formatProvider | The format provider. |
Returns
Type | Description |
---|---|
System.Int32 | A 32-bit signed integer that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for an System.Int32 type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Int32.MinValue or greater than System.Int32.MaxValue. |
ToInt64(Object)
Converts the value of the specified object to a 64-bit signed integer.
Declaration
public static long ToInt64(this object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
Returns
Type | Description |
---|---|
System.Int64 | A 64-bit signed integer that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for an System.Int64 type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Int64.MinValue or greater than System.Int64.MaxValue. |
ToInt64(Object, IFormatProvider)
Converts the specified string representation of a number to an equivalent 64-bit signed integer, using the specified culture-specific formatting information.
Declaration
public static long ToInt64(this object obj, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | An object that implements the System.IConvertible interface, or null. |
System.IFormatProvider | formatProvider | The format provider. |
Returns
Type | Description |
---|---|
System.Int64 | A 64-bit signed integer that is equivalent to value, or zero if value is null. |
Exceptions
Type | Condition |
---|---|
System.FormatException | value is not in an appropriate format for an System.Int64 type. |
System.InvalidCastException | value does not implement the System.IConvertible interface. Or the conversion is not supported. |
System.OverflowException | value represents a number that is less than System.Int64.MinValue or greater than System.Int64.MaxValue. |
ToSingleItemArray<T>(T)
Creates a array with the given value as only item.
Declaration
public static T[] ToSingleItemArray<T>(this T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value to add to the new created array. |
Returns
Type | Description |
---|---|
T[] | Returns the new created array. |
Type Parameters
Name | Description |
---|---|
T | The type of the items in the array. |