Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator with C# dynamic?

I have this function:

static void Func1<T>(T x, T y)
{
    dynamic result = ((dynamic)x + y); //line 1
    dynamic result2 = (x + y);         //line 2
}

This func can be executed as Func(1,2); However, line 1 is OK, while line 2 goes BANG (at compile time).

The exception thrown from line 2 is:

Operator '+' cannot be applied to operands of type 'T' and 'T'

So, we need to create an operator overload. Okay, so far so good.

But what about line 1? Shouldn't it need a dynamic cast also on y?

((dynamic)x + (dynamic)y);

I understand that it is being evaluated at runtime, but why does the C# compiler accept the + operator in line 1 (i.e. wrongly assume that T can be + to something else)?

like image 954
Royi Namir Avatar asked May 04 '12 16:05

Royi Namir


1 Answers

In your first example, by making x a dynamic you've in effect made the operator+ operation dynamic as well. This gets rid of the type specifier T for x, thereby getting rid of the complaint that T has no valid operator+.

At run time dynamic binding will occur and evaluate the two operands to ensure that operator+ can be used:

If an operand of an arithmetic operator has the compile-time type dynamic, then the expression is dynamically bound (§7.2.2). In this case the compile-time type of the expression is dynamic, and the resolution described below will take place at run-time using the run-time type of those operands that have the compile-time type dynamic.

In your second example, the compiler knows the types for x + y and is simply storing the result into a dynamic variable. Further usages of result2 will be dynamically bound. This makes sense as there are no dynamic operations to the right of the assignment operator:

When no dynamic expressions are involved, C# defaults to static binding, which means that the compile-time types of constituent expressions are used in the selection process.

like image 157
user7116 Avatar answered Sep 23 '22 05:09

user7116