Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Static and Dynamic Binding

Anonymous functions | Implicit conversions involving type parameters | Explicit numeric conversions | Explicit reference conversions | Unboxing conversions | Evaluation of user-defined conversions | User-defined explicit conversions | Anonymous function conversions | Implementation example | Method group conversions |


Читайте также:
  1. Analysis of volume and dynamics of commodity turnover
  2. Benign prostatic hyperplasia
  3. BINDING ON RAISED CORDS
  4. Binding the powers сплетіння
  5. Dynamic binding
  6. Dynamic memory allocation
  7. DYNAMIC MUSLI

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.

Binding-time

Static binding takes place at compile-time, whereas dynamic binding takes place at run-time. In the following sections, the term binding-time refers to either compile-time or run-time, depending on when the binding takes place.

The following example illustrates the notions of static and dynamic binding and of binding-time:

object o = 5;
dynamic d = 5;

Console.WriteLine(5); // static binding to Console.WriteLine(int)
Console.WriteLine(o); // static binding to Console.WriteLine(object)
Console.WriteLine(d); // dynamic binding to Console.WriteLine(int)

The first two calls are statically bound: the overload of Console.WriteLine is picked based on the compile-time type of their argument. Thus, the binding-time is compile-time.

The third call is dynamically bound: the overload of Console.WriteLine is picked based on the run-time type of its argument. This happens because the argument is a dynamic expression – its compile-time type is dynamic. Thus, the binding-time for the third call is run-time.


Дата добавления: 2015-11-16; просмотров: 39 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Expression classifications| Dynamic binding

mybiblioteka.su - 2015-2024 год. (0.006 сек.)