Static and Dynamic Binding
The process of determining the meaning of an operation based on the type or value of constituent expressions (arguments, operands, receivers) is often referred to as binding. For instance the meaning of a method call is determined based on the type of the receiver and arguments. The meaning of an operator is determined based on the type of its operands. In C# the meaning of an operation is usually determined at compile-time, based on the compile-time type of its constituent expressions. Likewise, if an expression contains an error, the error is detected and reported by the compiler. This approach is known as static binding. However, if an expression is a dynamic expression (i.e. has the type dynamic) this indicates that any binding that it participates in should be based on its run-time type (i.e. the actual type of the object it denotes at run-time) rather than the type it has at compile-time. The binding of such an operation is therefore deferred until the time where the operation is to be executed during the running of the program. This is referred to as dynamic binding. When an operation is dynamically bound, little or no checking is performed by the compiler. Instead if the run-time binding fails, errors are reported as exceptions at run-time. The following operations in C# are subject to binding: · Member access: e.M · Method invocation: e.M(e1,…,en) · Delegate invocaton: e(e1,…,en) · Element access: e[e1,…,en] · Object creation: new C(e1,…,en) · Overloaded unary operators: +, -,!, ~, ++, --, true, false · Overloaded binary operators: +, -, *, /, %, &, &&, |, ||,??, ^, <<, >>, ==,!=, >, <, >=, <= · Assignment operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= · Implicit and explicit conversions 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. However, when one of the constituent expressions in the operations listed above is a dynamic expression, the operation is instead dynamically bound.
|