Share via


Operator Overloading Support

For .NET Framework 3.5, the Windows Workflow Foundation rules engine now supports operator overloading in rules. The implementation of this support is similar to operator overloading in the .NET Framework. That is, you can overload arithmetic (add, subtract, multiply, divide, mod), relational (equal to, less than, greater than, less than or equal to, greater than or equal to), or bitwise (and, or) operators. When those operators are used, the type's implementation is called rather than the default implementation. For more information about operator overloading, see Operator Overloading Usage Guidelines.

Because the WF rules engine is built upon CodeDom and that CodeDom does not support unary operators, the WF rules framework does not support them either. This is the same for the "!=" (not equals to) operator.

Representation of unary operators are converted into binary expressions, that is –x is represented as 0-x. Therefore, if binary operator (-) is used in a rule definition, the expression –x calls the overloaded binary operator (-) method.

The following examples show how overloaded operators are invoked from workflow rules and declarative activity conditions. The following code shows a class, CustomInt, with two overloaded operators.

public class CustomInt
{
    public int Value { get; set; }

    public static bool operator <(CustomInt n1, CustomInt n2)
    {
        return n1.Value < n2.Value;
    }

    public static bool operator >(CustomInt n1, CustomInt n2)
    {
        return n1.Value > n2.Value;
    }
}

In this example, the workflow has two CustomInt members, Int1 and Int2, and the overloaded > operator is invoked.

IF this.Int1 > this.Int2
THEN System.Console.WriteLine(“Int1 > Int2”)

Overloaded operators can also be invoked from a declarative activity condition. This example shows how to invoke the overloaded > operator from a declarative activity condition.

this.Int1 > this.Int2

WF rules engine does not automatically use the CompareTo method if the object implements the IComparable interface. You are still required to call CompareTo directly if you want to use that functionality.

In addition, users can define implicit and explicit conversion operators with their class definitions. The WF rules framework allows the use of implicit and explicit conversion operators, following the C# rules that are specified in the C# specification.

Note

When using .NET Framework 3.0, implicit conversions are not supported, so the user-defined conversion is not honored and a different method may be called instead.

See Also

Other Resources

Rule Changes in .NET Framework 3.5