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

Anonymous functions

Integral types | Floating point types | The decimal type | Nullable types | The object type | Boxing conversions | Satisfying constraints | Expression tree types | Local variables | Definite assignment |


Читайте также:
  1. Anonymous function conversions
  2. Anonymous function expressions
  3. Anonymous object creation expressions
  4. Basic approaches to language investigation. The functions of language.
  5. Basic functions: CHOOSE
  6. Committee’s tasks, functions, rights and obligations

For a lambda-expression or anonymous-method-expression expr with a body (either block or expression) body:

· The definite assignment state of an outer variable v before body is the same as the state of v before expr. That is, definite assignment state of outer variables is inherited from the context of the anonymous function.

· The definite assignment state of an outer variable v after expr is the same as the state of v before expr.

The example

delegate bool Filter(int i);

void F() {
int max;

// Error, max is not definitely assigned
Filter f = (int n) => n < max;

max = 5;
DoWork(f);
}

generates a compile-time error since max is not definitely assigned where the anonymous function is declared. The example

delegate void D();

void F() {
int n;
D d = () => { n = 1; };

d();

// Error, n is not definitely assigned
Console.WriteLine(n);
}

also generates a compile-time error since the assignment to n in the anonymous function has no affect on the definite assignment state of n outside the anonymous function.

 

Variable references

A variable-reference is an expression that is classified as a variable. A variable-reference denotes a storage location that can be accessed both to fetch the current value and to store a new value.

variable-reference:
expression

In C and C++, a variable-reference is known as an lvalue.

Atomicity of variable references

Reads and writes of the following data types are atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. In addition, reads and writes of enum types with an underlying type in the previous list are also atomic. Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined types, are not guaranteed to be atomic. Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.


Conversions

A conversion enables an expression to be treated as being of a particular type. A conversion may cause an expression of a given type to be treated as having a different type, or it may cause an expression without a type to get a type. Conversions can be implicit or explicit, and this determines whether an explicit cast is required. For instance, the conversion from type int to type long is implicit, so expressions of type int can implicitly be treated as type long. The opposite conversion, from type long to type int, is explicit and so an explicit cast is required.

int a = 123;
long b = a; // implicit conversion from int to long
int c = (int) b; // explicit conversion from long to int

Some conversions are defined by the language. Programs may also define their own conversions (§6.4).


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


<== предыдущая страница | следующая страница ==>
Simple assignment expressions| Implicit conversions involving type parameters

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