Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Technical documentation

jbuechner edited this page · 5 revisions
Clone this wiki locally

Cascade comes with a few namespaces and types used to group various extension methods. This documentation is focused on the description of namespaces and methods, so you can make the right choice which method and overload to use.

Table of contents

(skip)

Namespaces

Cascade consists only of few namespaces. The reason behind this is simple. In .NET you have to import a namespace before the extension methods are usable. Because most of the extension methods can be of use in almost every situation, one great namespace Cascade exists. This namespace contains merely all extension methods.

  • Cascade This namespace contains extension methods which are applicable on objects, various types by using generics or value types. Most extension methods are in this namespace.
  • Cascade.Data This namespace exposes extension methods which are more or less specific to the System.Data namespace. If you need to perform data-related actions, you should consider importing this namespace too.

Types

All extension methods are inside a type. As these are static classes which do nothing further than providing extension methods, descriptions for these classes are ommited.

Extension Methods

The core of Cascade are extension methods. The methods can be used to fulfil various tasks inside your code and allow you to use method chaining.

GenericExtensions (inside Cascade namespace)

This type contains extensions usable on kind of types using generics. Some extensions are only usable on reference types, while others are usable on all.

Invoke

This extension performs a number of invocations on a value. Each delegate passed to this extension is invoked in sequential order. The extension will not do anything if there are no delegates passed to it. However, the delegates are still invoked, event if the value is null.

Example (C#)

// With Cascade
someObject.Invoke(t => t.DoSomething(), t => t.DoSomethingOther());
// Without Cascade
someObject.DoSomething();
someObject.DoSomethingOther();

The extension has an overload which takes a Func<T, T> delegate instead of an Action<T> delegate. This overload is much more interesting. Each delegate invoked, returns reuslt value, which is used then for the next invocation. The result value of this extension is the result of the last delegate invoked.

Example (C#)

// With Cascade
int value = 1;
value =  value.Invoke(t => { return t + 1; }, t => { return t * 4; }, t => { return t / 2; }); // 8
// Without Cascade
value = ((value + 1) * 4) / 8);

GetValueOrNew

The extension can be used to either get the current value or a new value. This extension method works only for types with a parameterless constructor.

Example (C#)

// With Cascade
value = someObject.GetValueOrNew();
// Without Cascade
value = someObject ?? new object();

GetValueOrDefault

The extension is used to return a default value if the value itself is null. If the value is not null, the value itself is returned.

Example (C#)

// With Cascade
value = someObject.GetValueOrDefault(new object());
// Without Cascade
value = someObject ?? new object();

IfNotNull

This extension takes a delegeate. The delegate itself is only called if the value is not null. There are overloads for either Action<T> or Func<TIn, TOut> delegates, depending on your needs. The extension will take care of null references. If the delegate is null, the extension will do nothing.

Example (C#)

// With Cascade
someObject.IfNotNull(t => t.DoSomething());
object result = someObject.IfNotNull(t => { return t.GetSomething(); });
// Without Cascade
if (someObject != null)
    someObject.DoSomething();
object result = null;
if (someObject != null)
    someObject = someObject.GetSomething();

IfNull

This extension takes a single delegate which is used if the value itself is null. This extension is the counterpart to IfNotNull. There are overloads for either Action<T> or Func<T> depending on your needs. The extension will take care of null references. If the delegate is null, the extension will do nothing.

// With Cascade
someObject.IfNull(() => someObject = new object());
// Without Cascade
if (someObject == null)
    someObject = new object();

ObjectExtensions (inside Cascade namespace)

This type contains extension used on all types and values in .NET (types which derive from Object). The type contains cast functions like To... and As.... While the To... functions will perform a static cast, which raise an exception if the cast is not possible, the As... functions will perform a dynamic cast, which return the default value of the target type of the cast.

To<T>

The To extension is used to perform a static cast on a value. Static casts will raise exceptions when used on values which are not of the target type.

Example (C#)

// With Cascade
someObject.To<MyClass>();
// Without Cascade
(MyClass)someObject;

As<T>

The As extension performs a dynamic cast. In difference to the To extension, this will not raise an exception if the value can not be casted to the target type. Instead it returns the default value of the target type if the value can't be cast.

In difference to the C# operatior as this can even be applied to value types.

Example (C#)

// With Cascade
someObject.As<MyClass>();
// Without Cascade
someObject as MyClass;

IsNull

The IsNull extension checks whether the given value is null. This can be applied on all types and uses the == operator. If you've overloaded the == operator in your custome type, the semantic of this call can be changed.

Example (C#)

// With Cascade
someObject.IsNull();
// Without Cascade
someObject == null;

StaticCast<T>

Performs a static cast, or if not possible, delegates work to a callback and returns the result. In difference to the To extension, StaticCast will first check if the value can be casted into the target type. If possible it will return the casted value. If it's not possible, a callback method is called and the result value of this method is returned. This can be usefull, if you want to perform a cast, or if's not possible to create either dummy or new objects of the desired type. It can even be helpful if you've written a custom conversion method for your target type.

The first parameter of the callback function is the value on which the StaticCast extension is performed.

Example (C#)

// With Cascade
someObject.StaticCast<MyClass>(MyCallbackIfStaticCastIsNotPossible);
// Without Cascade
MyClass value = someObject as MyClass;
if (value == null)
    value = MyCallbackIfStaticCastIsNotPossible(someObject);

DynamicCast<T>

The dynamic cast performs a cast on a value if possible and returns the value in the specific target type of the cast. In difference to the As extension, this extension can use a callback if the cast can not be done. The method will cast the value directly if the cast is possible, or null if the value itself is null. In any other case the result value of a given callback function is returned.

The first parameter of the callback function is the value on which the DynamicCast extension is performed. The following exceptions are catched when raised by the callback: FormatException, InvalidCastException, OverflowException. If any of these exceptions occur, the default value of the target type will be returned instead.

Example (C#)

// With Cascade
someObject.DynamicCast<MyClass>(MyCallbackIfDynamicCastIsNotPossible);
// Without Cascade
MyClass value = someObject as MyClass;
if (value == null)
    value = MyCallbackIfDynamicCastIsNotPossible(someObject);

ToBoolean

This method performs a static cast and returns a boolean value. If the value is not of type Boolean, the System.Convert.ToBoolean(object) method is used for conversion. ToBoolean has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToBoolean.

Example (C#)

// With Cascade
someObject.ToBoolean();
// Without Cascade
bool value;
if (someObject is bool)
    value = (bool)someObject;
else
    value = System.Convert.ToBoolean(someObject);

ToByte

This method performs a static cast and returns a(n) (unsigned) byte value. If the value is not of type Boolean, the System.Convert.ToByte(object) method is used for conversion. ToByte has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToByte.

Example (C#)

// With Cascade
someObject.ToByte();
// Without Cascade
byte value;
if (someObject is byte)
    value = (byte)someObject;
else
    value = System.Convert.ToByte(someObject);

ToSByte

This method performs a static cast and returns a signed byte value. If the value is not of type Boolean, the System.Convert.ToSByte(object) method is used for conversion. ToSByte has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToSByte.

Example (C#)

// With Cascade
someObject.ToByte();
// Without Cascade
sbyte value;
if (someObject is sbyte)
    value = (sbyte)someObject;
else
    value = System.Convert.ToSByte(someObject);

ToChar

This method performs a static cast and returns a char value. If the value is not of type Char, the System.Convert.ToChar(object) method is used for conversion. ToChar has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToChar.

Example (C#)

// With Cascade
someObject.ToChar();
// Without Cascade
char value;
if (someObject is char)
    value = (char)someObject;
else
    value = System.Convert.ToChar(someObject);

ToDateTime

This method performs a static cast and returns a DateTime value. If the value is not of type Char, the System.Convert.ToDateTime(object) method is used for conversion. ToDateTime has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToDateTime.

If the casted value a positive integer value, the value is used to create a new DateTime where the value is used as ticks for the DateTime.

Example (C#)

// With Cascade
someObject.ToDateTime();
// Without Cascade
DateTime value;
if (someObject is DateTime)
    value = (DateTime)someObject;
else
    value = System.Convert.ToDateTime(someObject);

ToTimeSpan

This method performs a static cast and returns a timespan value. As there is no System.Convert.ToTimeSpan(object) method, the timespan is parsed instead with the TimeSpan.Parse method. The method uses the current globalization (System.Thread.CurrentThread.CurrentCulture) for parsing. There is no overload (as with .NET framework 3.5) to specify an IFormatProvider.

If the casted value an integer value, the value is used to create a new TimeSpan where the value is used as ticks for the TimeSpan.

Example (C#)

// With Cascade
someObject.ToTimeSpan();
// Without Cascade
TimeSpan value;
if (someObject is TimeSpan)
    value = (TimeSpan)someObject;
else
    if (someObject != null)
        value = TimeSpan.Parse(someObject.ToString());

ToDecimal

This method performs a static cast and returns a decimal value. If the value is not of type Decimal, the System.Convert.ToDecimal(object) method is used for conversion. ToDecimal has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToDecimal.

Example (C#)

// With Cascade
someObject.ToDecimal();
// Without Cascade
char value;
if (someObject is Decimal)
    value = (Decimal)someObject;
else
    value = System.Convert.ToDecimal(someObject);

ToDouble

This method performs a static cast and returns a double precision floating number. If the value is not of type Double, the System.Convert.ToDouble(object) method is used for conversion. ToDouble has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToDouble.

Example (C#)

// With Cascade
someObject.ToDouble();
// Without Cascade
double value;
if (someObject is Decimal)
    value = (double)someObject;
else
    value = System.Convert.ToDouble(someObject);

ToSingle

This method performs a static cast and returns a single precision floating number. If the value is not of type Single, the System.Convert.ToSingle(object) method is used for conversion. ToSingle has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToSingle.

Example (C#)

// With Cascade
someObject.ToSingle();
// Without Cascade
float value;
if (someObject is float)
    value = (float)someObject;
else
    value = System.Convert.ToSingle(someObject);

ToInt16

This method performs a static cast and returns a two byte wide number. If the value is not of type Int16, the System.Convert.ToInt16(object) method is used for conversion. ToInt16 has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToInt16.

Example (C#)

// With Cascade
someObject.ToInt16();
// Without Cascade
short value;
if (someObject is short)
    value = (short)someObject;
else
    value = System.Convert.ToInt16(someObject);

ToInt16

This method performs a static cast and returns a two byte wide unsigned number. If the value is not of type ToUInt16, the System.Convert.ToUInt16(object) method is used for conversion. ToUInt16 has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToUInt16.

Example (C#)

// With Cascade
someObject.ToUInt16();
// Without Cascade
ushort value;
if (someObject is ushort)
    value = (ushort)someObject;
else
    value = System.Convert.ToUInt16(someObject);

ToInt32

This method performs a static cast and returns a four byte wide number. If the value is not of type Int32, the System.Convert.ToInt32(object) method is used for conversion. ToInt32 has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToInt32.

Example (C#)

// With Cascade
someObject.ToInt32();
// Without Cascade
int value;
if (someObject is int)
    value = (int)someObject;
else
    value = System.Convert.ToInt32(someObject);

ToUInt32

This method performs a static cast and returns a four byte wide unsigned number. If the value is not of type UInt32, the System.Convert.ToUInt32(object) method is used for conversion. ToUInt32 has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToUInt32.

Example (C#)

// With Cascade
someObject.ToUInt32();
// Without Cascade
uint value;
if (someObject is uint)
    value = (uint)someObject;
else
    value = System.Convert.ToUInt32(someObject);

ToInt64

This method performs a static cast and returns an eight byte wide number. If the value is not of type Int64, the System.Convert.ToInt64(object) method is used for conversion. ToInt64 has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToInt64.

Example (C#)

// With Cascade
someObject.ToInt64();
// Without Cascade
long value;
if (someObject is long)
    value = (long)someObject;
else
    value = System.Convert.ToInt64(someObject);

ToUInt64

This method performs a static cast and returns an eight byte wide unsigned number. If the value is not of type UInt64, the System.Convert.ToUInt64(object) method is used for conversion. ToUInt64 has an overload to pass an IFormatProvider in. The provider is used when performing a conversion with System.Convert.ToUInt64.

Example (C#)

// With Cascade
someObject.ToUInt64();
// Without Cascade
ulong value;
if (someObject is ulong)
    value = (ulong)someObject;
else
    value = System.Convert.ToUInt64(someObject);

AsBoolean

This method performs a dynamic cast and returns a nullable boolean value. Value will be static casted if its of type Boolean. In any other case an System.Convert.ToBoolean(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToBoolean(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsBoolean();
// Without Cascade
bool? value;
if (someObject is bool)
    value = (bool)someObject;
else
    try
    {
        value = System.Convert.ToBoolean(someObject);
    }
    catch
    {
    }

AsByte

This method performs a dynamic cast and returns a nullable unsigned byte value. Value will be static casted if its of type Byte. In any other case an System.Convert.ToByte(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToByte(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsByte();
// Without Cascade
bool? value;
if (someObject is byte)
    value = (byte)someObject;
else
    try
    {
        value = System.Convert.ToByte(someObject);
    }
    catch
    {
    }

AsSByte

This method performs a dynamic cast and returns a nullable unsigned byte value. Value will be static casted if its of type SByte. In any other case an System.Convert.ToSByte(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToSByte(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsSByte();
// Without Cascade
sbool? value;
if (someObject is sbyte)
    value = (sbyte)someObject;
else
    try
    {
        value = System.Convert.ToSByte(someObject);
    }
    catch
    {
    }

AsChar

This method performs a dynamic cast and returns a nullable char value. Value will be static casted if its of type Char. In any other case an System.Convert.ToChar(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToChar(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsChar();
// Without Cascade
char? value;
if (someObject is char)
    value = (char)someObject;
else
    try
    {
        value = System.Convert.ToChar(someObject);
    }
    catch
    {
    }

AsDateTime

This method performs a dynamic cast and returns a nullable datetime value. Value will be static casted if its of type DateTime. In any other case an System.Convert.ToDateTime(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToDateTime(object) for the conversion.

If the casted value is a positive numerical value, the value is used as Ticks to create a new DateTime.

Example (C#)

// With Cascade
someObject.AsDateTime();
// Without Cascade
DateTime? value;
if (someObject is DateTime)
    value = (DateTime)someObject;
else
    try
    {
        value = System.Convert.ToDateTime(someObject);
    }
    catch
    {
    }

AsTimeSpan

This method performs a dynamic cast and returns a nullable timespan value. Value will be static casted if its of type TimeSpan. As there is not System.Convert.ToTimeSpan(object) method (with 3.5 .NET framework), the TimeSpan.Parse method is used instead. This is the reason why there is not overload with IFormatProvider, be

If the casted value is a numerical value, the value is used as Ticks to create a new TimeSpan.

Example (C#)

// With Cascade
someObject.AsDateTime();
// Without Cascade
TimeSpan? value;
if (someObject is TimeSpan)
    value = (TimeSpan)someObject;
else
    try
    {
        value = TimeSpan.Parse(someObject.ToString());
    }
    catch
    {
    }

AsDecimal

This method performs a dynamic cast and returns a nullable decimal value. Value will be static casted if its of type Decimal. In any other case an System.Convert.ToDecimal(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToDecimal(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsDecimal();
// Without Cascade
decimal? value;
if (someObject is decimal)
    value = (decimal)someObject;
else
    try
    {
        value = System.Convert.ToDecimal(someObject);
    }
    catch
    {
    }

AsDouble

This method performs a dynamic cast and returns a nullable double precision floating point value. Value will be static casted if its of type Double. In any other case an System.Convert.ToDouble(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToDouble(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsDouble();
// Without Cascade
double? value;
if (someObject is double)
    value = (double)someObject;
else
    try
    {
        value = System.Convert.ToDouble(someObject);
    }
    catch
    {
    }

AsSingle

This method performs a dynamic cast and returns a nullable single precision floating point value. Value will be static casted if its of type Single. In any other case an System.Convert.ToSingle(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToSingle(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsSingle();
// Without Cascade
float? value;
if (someObject is float)
    value = (float)someObject;
else
    try
    {
        value = System.Convert.ToSingle(someObject);
    }
    catch
    {
    }

AsInt16

This method performs a dynamic cast and returns a nullable two byte wide numerical value. Value will be static casted if its of type Int16. In any other case an System.Convert.ToInt16(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToInt16(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsInt16();
// Without Cascade
short? value;
if (someObject is short)
    value = (short)someObject;
else
    try
    {
        value = System.Convert.ToInt16(someObject);
    }
    catch
    {
    }

AsUInt16

This method performs a dynamic cast and returns a nullable two byte wide unsigned numerical value. Value will be static casted if its of type UInt16. In any other case an System.Convert.ToUInt16(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToUInt16(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsUInt16();
// Without Cascade
ushort? value;
if (someObject is ushort)
    value = (ushort)someObject;
else
    try
    {
        value = System.Convert.ToUInt16(someObject);
    }
    catch
    {
    }

AsInt32

This method performs a dynamic cast and returns a nullable four byte wide numerical value. Value will be static casted if its of type Int32. In any other case an System.Convert.ToInt32(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToInt32(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsInt32();
// Without Cascade
int? value;
if (someObject is int)
    value = (int)someObject;
else
    try
    {
        value = System.Convert.ToInt32(someObject);
    }
    catch
    {
    }

AsUInt32

This method performs a dynamic cast and returns a nullable four byte wide unsigned numerical value. Value will be static casted if its of type UInt32. In any other case an System.Convert.ToUInt32(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToUInt32(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsUInt32();
// Without Cascade
uint? value;
if (someObject is uint)
    value = (uint)someObject;
else
    try
    {
        value = System.Convert.ToUInt32(someObject);
    }
    catch
    {
    }

AsInt64

This method performs a dynamic cast and returns an nullable eight byte wide numerical value. Value will be static casted if its of type Int64. In any other case an System.Convert.ToInt64(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToInt64(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsInt64();
// Without Cascade
long? value;
if (someObject is long)
    value = (long)someObject;
else
    try
    {
        value = System.Convert.ToInt64(someObject);
    }
    catch
    {
    }

AsUInt64

This method performs a dynamic cast and returns an nullable eight byte wide unsigned numerical value. Value will be static casted if its of type UInt64. In any other case an System.Convert.ToUInt64(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToUInt64(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsUInt64();
// Without Cascade
ulong? value;
if (someObject is ulong)
    value = (ulong)someObject;
else
    try
    {
        value = System.Convert.ToUInt64(someObject);
    }
    catch
    {
    }

StructExtensions (inside Cascade namespace)

This type contains extension usable on all structures.

IfNotNull

This extension can be used on nullable value types (Nullable<T>). You can pass a delegate to this extension which is only executed if the value is null. The extension provides overloads for an ordinary Action (Action<T>) or an function returning a result (Func<TIn, TOut>). If the value is null the value itself will be returned without modifying it. The extension method will take care of null references. If the delegate is null the method will just return the value on which the extension method is called.

Example (C#)

// With Cascade
int? value = 0;
value.IfNotNull(v => value = v + 1); // Action<T>
value = value.IfNotNull(v => { return v + 1}); // Func<TIn, TOut>
// Without Cascade
int? value = 0;
if (value != null)
    value = value + 1;

IfNull

This extension is the counterpart to IfNotNull. The extension takes a delegate which is only used if the nullable value is null. The extension has an overload for a simple action (Action) or a function with return value (Function<TOut>). Both delegates have no in parameter, because it makes no sense if the value is null, doesn't it? If the value is not null the value itself will be returned without modifying it. The extension method will take care of null references. If the delegate is null the method will just return the value on which the extension method is called.

Example (C#)

// With Cascade
int? value = null;
value.IfNull(() => value = 1); // Action
value = value.IfNull(() => { return 1}); // Func<Out>
// Without Cascade
int? value = null;
if (value == null)
    value = 1;

HasFlag

The extension determines whether a flag is set on a value or not. The extension can operate on enums or on integral value types (except Char). Flags are used to store values as bits in a number. Each bit in the number is associated with a flag. Since .NET framework 4.0 there is an Enum.HasFlag method for enums. This extension however can even operate on integral value types without being an enum.

// With Cascade
FlagEnum enumValue = FlagEnum.A | FlagEnum.B;
hasFlag = enumValue.HasFlag(FlagEnum.A);
int value = 1 | 2;
hasFlag = value.HasFlag(2);
// Without Cascade
FlagEnum enumValue = FlagEnum.A | FlagEnum;
hasFlag = enumValue == (enumValue | FlagEnum.A);
int value = 1 | 2;
hasFlag = value == (value | 2);

EnumerableExtensions (inside Cascade namespace)

This type contains extensions which operates on types deriving from IEnumerable or IEnumerable<T>.

ForEach

This extension method takes an delegate with the Action<T> signature. The delegate is used for each item in the enumeration. The first parameter of the delegate is the current item of the enumeration. The is an overload operating on the generic IEnumerable<T> types and on the old untyped IEnumerable types. The extension method takes care of null references. If either the enumeration itself or the delegate is null, the extension will safely return without any action taken.

Example (C#)

// With Cascade
int sum = 0;
someIntEnumeration.ForEach(item => sum += item);
// Without Cascade
int sum = 0;
if (someIntEnumeration != null)
    foreach (int item in someIntEnumeration)
        sum += item;

Cast

This extension method operates on an IEnumerable<T> to return a new enumeration from another type. Since .NET 4.0 the framework allow some contra- and covariance when working with enumerations. However this will not help you in all cases. With this extension you've the freedom to cast enumeration values to any other type using a delegate for casting. This method will take care of null references. If either the enumeration or the cast delegate is null, the method will just return.

Example (C#)

// With Cascade
someIntEnumeration.Cast(item => { return item.ToString() });
// Without Cascade
List<string> castedValues = new List<string>();
if (someIntEnumeration != null)
    foreach (int item in someIntEnumeration)
        castedValues.Add(item.ToString());

DictionaryExtensions (inside Cascade namespace)

This type contains extensions usable on all types implementing the IDictionary<TKey, TValue> interface.

Get

This method is used to retreive a value from a dictionary using its key. To access a dictionary in a safe manner you must make sure the key is inside the dictionary and then you can return the value. This method is a shorthand for this two operations. In addition it takes care if the dictionary is null. The method will return null if either the dictionary is null, the key is not present in the dictionary or if the value in the dictionary itself is null. In any other case an actual value.

Example (C#)

// With Cascade
someDictionary.Get(key);
// Without Cascade
object value = null;
if (someDictionary != null && someDictionary.ContainsKey(key))
    value = someDictionary[key];

This method provides an overload, taking an delegate with the Func<TValue> signature. The delegate is used if the key is not present in the dictionary. The result value of this delegate is then added to the dictionary and returned. If the delegate is null, the overload behaves exactly like the one without this delegate.

Example (C#)

// With Cascade
someDictionary.Get(key, callbackIfKeyIsNotPresent);
// Without Cascade
object value = null;
if (someDictionary != null)
    if (someDictionary.ContainsKey(key))
        value = someDictionary[key];
    else
    {
        if (callbackIfKeyIsNotPresent != null)
        {
            value = callbackIfKeyIsNotPresent();
            dictionary.Add(key, value);
        }

DatabaseObjectExtensions (inside Cascade.Data namespace)

This type contains extensions usable on all types and values in .NET (derived from System.Object). The type contains dynamic cast functions which are special made for usage with System.Data namespace (handling System.DBNull values). Static casts are not available for this special case. For static casts you can use the extensions from the Cascade namespace.

ToDbValue

This method will return a value that can be used in a database-related context (like creating parameters for an adapter). The method will return the value if it is not null, otherwise DBNull. The reason is, most classes in System.Data namespace expecting DBNull instead of null if an null value is used in a database context.

Example (C#)

// With Cascade
someObject.ToDbValue();
// Without Cascade
object value = someObject ?? DBNull.Value;

DbIsNull

This method checks whether value is null or DBNull. In both cases true is returned. In any other case false.

Example (C#)

// With Cascade
bool isNull = someObject.DbIsNull();
// Without Cascade
bool isNull = someObject == null || Convert.IsDBNull(someObject);

DbAsBoolean

This method performs a dynamic cast and returns a nullable boolean value. Value will be static casted if its of type Boolean. If the value is DBNull null is returned. In any other case an System.Convert.ToBoolean(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToBoolean(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsBoolean();
// Without Cascade
bool? value = null;
if (someObject is bool)
    value = (bool)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToBoolean(someObject);
        }
        catch
        {
        }

DbAsByte

This method performs a dynamic cast and returns a nullable unsigned byte value. Value will be static casted if its of type Byte. If the value is DBNull null is returned. In any other case an System.Convert.ToByte(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToByte(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsByte();
// Without Cascade
byte? value = null;
if (someObject is byte)
    value = (byte)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToByte(someObject);
        }
        catch
        {
        }

DbAsSByte

This method performs a dynamic cast and returns a nullable unsigned byte value. Value will be static casted if its of type SByte. If the value is DBNull null is returned. In any other case an System.Convert.ToSByte(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToSByte(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsSByte();
// Without Cascade
sbyte? value = null;
if (someObject is sbyte)
    value = (sbyte)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToSByte(someObject);
        }
        catch
        {
        }

DbAsChar

This method performs a dynamic cast and returns a nullable char value. Value will be static casted if its of type Char. If the value is DBNull null is returned. In any other case an System.Convert.ToChar(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToChar(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsChar();
// Without Cascade
char? value = null;
if (someObject is char)
    value = (char)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToChar(someObject);
        }
        catch
        {
        }

DbAsDateTime

This method performs a dynamic cast and returns a nullable datetime value. Value will be static casted if its of type DateTime. If the value is DBNull null is returned. In any other case an System.Convert.ToDateTime(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToDateTime(object) for the conversion.

If the casted value is a positive numerical value, the value is used as Ticks to create a new DateTime.

Example (C#)

// With Cascade
someObject.DbAsDateTime();
// Without Cascade
DateTime? value = null;
if (someObject is DateTime)
    value = (DateTime)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToDateTime(someObject);
        }
        catch
        {
        }

DbAsTimeSpan

This method performs a dynamic cast and returns a nullable timespan value. Value will be static casted if its of type TimeSpan. If the value is DBNull null is returned. As there is not System.Convert.ToTimeSpan(object) method (with 3.5 .NET framework), the TimeSpan.Parse method is used instead. This is the reason why there is not overload with IFormatProvider, be

If the casted value is a numerical value, the value is used as Ticks to create a new TimeSpan.

Example (C#)

// With Cascade
someObject.DbAsTimeSpan();
// Without Cascade
TimeSpan? value = null;
if (someObject is TimeSpan)
    value = (TimeSpan)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = TimeSpan.Parse(someObject.ToString());
        }
        catch
        {
        }

DbAsDecimal

This method performs a dynamic cast and returns a nullable decimal value. Value will be static casted if its of type Decimal. If the value is DBNull null is returned. In any other case an System.Convert.ToDecimal(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToDecimal(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsDecimal();
// Without Cascade
decimal? value;
if (someObject is decimal)
    value = (decimal)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToDecimal(someObject);
        }
        catch
        {
        }

DbAsDouble

This method performs a dynamic cast and returns a nullable double precision floating point value. Value will be static casted if its of type Double. If the value is DBNull null is returned. In any other case an System.Convert.ToDouble(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToDouble(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsDouble();
// Without Cascade
double? value;
if (someObject is double)
    value = (double)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToDouble(someObject);
        }
        catch
        {
        }

DbAsSingle

This method performs a dynamic cast and returns a nullable single precision floating point value. Value will be static casted if its of type Single. If the value is DBNull null is returned. In any other case an System.Convert.ToSingle(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToSingle(object) for the conversion.

Example (C#)

// With Cascade
someObject.AsSingle();
// Without Cascade
float? value;
if (someObject is float)
    value = (float)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToSingle(someObject);
        }
        catch
        {
        }

DbAsInt16

This method performs a dynamic cast and returns a nullable two byte wide numerical value. Value will be static casted if its of type Int16. If the value is DBNull null is returned. In any other case an System.Convert.ToInt16(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToInt16(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsInt16();
// Without Cascade
short? value = null;
if (someObject is short)
    value = (short)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToInt16(someObject);
        }
        catch
        {
        }

DbAsUInt16

This method performs a dynamic cast and returns a nullable two byte wide unsigned numerical value. Value will be static casted if its of type UInt16. If the value is DBNull null is returned. In any other case an System.Convert.ToUInt16(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToUInt16(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsUInt16();
// Without Cascade
ushort? value = null;
if (someObject is ushort)
    value = (ushort)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToUInt16(someObject);
        }
        catch
        {
        }

DbAsInt32

This method performs a dynamic cast and returns a nullable four byte wide numerical value. Value will be static casted if its of type Int32. If the value is DBNull null is returned. In any other case an System.Convert.ToInt32(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToInt32(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsInt32();
// Without Cascade
int? value = null;
if (someObject is int)
    value = (int)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToInt32(someObject);
        }
        catch
        {
        }

DbAsUInt32

This method performs a dynamic cast and returns a nullable four byte wide unsigned numerical value. Value will be static casted if its of type UInt32. If the value is DBNull null is returned. In any other case an System.Convert.ToUInt32(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToUInt32(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsUInt32();
// Without Cascade
uint? value = null;
if (someObject is uint)
    value = (uint)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToUInt32(someObject);
        }
        catch
        {
        }

DbAsInt64

This method performs a dynamic cast and returns an nullable eight byte wide numerical value. Value will be static casted if its of type Int64. If the value is DBNull null is returned. In any other case an System.Convert.ToInt64(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToInt64(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsInt64();
// Without Cascade
long? value = null;
if (someObject is long)
    value = (long)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToInt64(someObject);
        }
        catch
        {
        }

DbAsUInt64

This method performs a dynamic cast and returns an nullable eight byte wide unsigned numerical value. Value will be static casted if its of type UInt64. If the value is DBNull null is returned. In any other case an System.Convert.ToUInt64(object) is used for conversion. If the conversion is not possible, null is returned.

An overload of this extension method takes an IFormatProvider which is passed to System.Convert.ToUInt64(object) for the conversion.

Example (C#)

// With Cascade
someObject.DbAsUInt64();
// Without Cascade
ulong? value = null;
if (someObject is ulong)
    value = (ulong)someObject;
else
    if (someObject != null && !Convert.IsDBNull(someObject))
        try
        {
            value = System.Convert.ToUInt64(someObject);
        }
        catch
        {
        }
Something went wrong with that request. Please try again.