In a API for accessing a custom data source, I wanted to add Lightspeed-like query functionality.

Consider the following example call to a ‘Find’ method, that queries the datasource with conditions for the ‘title’ and ‘size’ fields:

Find(Entity.Field("Title") == "Onderweg" && Entity.Field("Size") > 300);

One of the techniques I used to implement the Find method, are custom predicate (expression) trees. The argument for the Find method is a custom query expression, that is translated into a native query language. Note that I did choose not to use Linq expressions, because field names in filter expressions needed to be dynamic (string values).

Another used technique – and the subject of this post – is operator overloading.

Why operator overloading?

The Entity.Field() method returns a custom query object of type QueryExpression. The two QueryExpression objects in the example above are combined by the ‘&&’ operator. Normally, ‘&&’ is used on boolean expressions, and returns true if both expressions are true. But in the our case the ‘&&’ operator is not used on booleans but on a custom type (a ‘QueryExpression’). Therefore, we need to overload the default behavior of the ‘&&’ operator.

Implementing overloading

In C# you can overload operators to define operations on user defined types, but the && and operators || can’t be directly overloaded. So, this code is won’t compile:

public static bool operator &&(Car Car1, Car Car2) {
   // Custom operations
}

However, if the following two conditions are met, short-circuit operators like ‘&&’ are automatically made available:

  • The custom class must overload the normal logical operators (& and |)
  • The true and false operators must be overloaded.

This is possible because the operation for ‘&&’ can be derived from the operation for ‘&’:

The operation x && y corresponds to the operation x & y, except that if x is false, y is not evaluated (because the result of the AND operation is false no matter what the value of y may be). This is known as “short-circuit” evaluation.

Source: MSDN

Example code for overloading & and | operators:

public static QueryExpression operator &(QueryExpression q1, QueryExpression q2) {
    QueryExpression result = new PredicateQueryExpression();
    result.LeftHandSide = q1;
    result.RighthandSide = q2;
    result.Operator = Operators.And;
    return result;
}

public static QueryExpression operator |(QueryExpression q1, QueryExpression q2) {
    QueryExpression result = new PredicateQueryExpression();
    result.LeftHandSide = q1;
    result.RightHandSide = q2;
    result.Operator = Operators.Or;
    return result;
}

See also:

3 Responses to “C# operator overloading: the ‘&&’ operator”

  1. usman riaz kiani says:

    thanks for helping i want some more help if you please help me

  2. jitendra says:

    Hi sir Good evening Is it possible overloading conditional operator?

  3. jitendra says:

    Hi sir good evening is it possible overloading conditional operator?

Leave a Reply





Human Verification