How to: Use Lambda Expressions Outside LINQ



Lambda expressions are not limited to LINQ queries. You can use them anywhere a delegate value is expected, that is, wherever an anonymous method can be used. The following example shows how to use a lambda expression in a Windows Forms event handler. Notice that the types of the inputs (Object and MouseEventArgs) are inferred by the compiler and do not have to be explicitly given in the lambda input parameters.

Example

public partial class Form1 : Form { public Form1() {    InitializeComponent();    // Use a lambda expression to define an event handler.   this.Click += (s, e) => { MessageBox.Show(((MouseEventArgs)e).Location.ToString());}; } }

 


Использование лямбда-выражений вне LINQ

Лямбда-выражения не ограничиваются запросами LINQ. Их можно использовать везде, где ожидается значение-делегат, то есть везде, где можно использовать анонимные методы. В следующем примере показано, как использовать лямбда-выражение в обработчике событий Windows Forms. Обратите внимание, что входные типы (Object и MouseEventArgs) выводятся компилятором, их не нужно явным образом указывать во входных параметрах лямбда-выражения.

Пример

public partial class Form1 : Form { public Form1() {    InitializeComponent();    // Use a lambda expression to define an event handler.   this.Click += (s, e) => { MessageBox.Show(((MouseEventArgs)e).Location.ToString());}; } }

 


Anonymous Methods

In versions of C# before 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code. However, the information about anonymous methods in this topic also applies to lambda expressions. There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list, and this means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions. For more information specifically about lambda expressions, see Lambda Expressions.

Creating anonymous methods is essentially a way to pass a code block as a delegate parameter. Here are two examples:

// Create a handler for a click event button1.Click += delegate(System.Object o, System.EventArgs e)               { System.Windows.Forms.MessageBox.Show("Click!"); };
// Create a delegate instance delegate void Del(int x);   // Instantiate the delegate using an anonymous method Del d = delegate(int k) { /* ... */ };

By using anonymous methods, you reduce the coding overhead in instantiating delegates because you do not have to create a separate method.

For example, specifying a code block instead of a delegate can be useful in a situation when having to create a method might seem an unnecessary overhead. A good example would be when you start a new thread. This class creates a thread and also contains the code that the thread executes without creating an additional method for the delegate.

void StartThread() { System.Threading.Thread t1 = new System.Threading.Thread (delegate()        {            System.Console.Write("Hello, ");            System.Console.WriteLine("World!");        }); t1.Start(); }

Анонимные методы

Именованные методы были единственным способом объявления делегата в версиях C#, предшествующих версии 2.0. Анонимные методы были представлены в C# 2.0, а в версиях C# 3.0 и более поздних лямбда-выражения заменяют эти методы и являются предпочтительным способом написания встроенного кода. Однако сведения об анонимных методах, представленные в данном разделе, применяются и к лямбда-выражениям. Существует только один случай, в котором функциональность анонимного метода отсутствует в лямбда-выражениях. Анонимные методы позволяют отказаться от использования списка параметров, а это значит, что анонимный метод может быть преобразован в делегаты с различными подписями. Это невозможно в ситуации с лямбда-выражениями. Дополнительные сведения именно о лямбда-выражениях см. в разделе Лямбда-выражения.

Создание анонимных методов является, по существу, способом передачи блока кода в качестве параметра делегата. Далее представлено два примера.

ß---

Использование анонимных методов позволяет сократить издержки на кодирование при создании делегатов, поскольку не требуется создавать отдельный метод.

Например, указание блока кода вместо делегата может быть целесообразно в ситуации, когда создание метода может показаться ненужным действием. Хорошим примером является запуск нового потока. Этот класс создает поток и содержит код, выполняемый потоком без создания дополнительного метода для его делегата.

ß---


Remarks

The scope of the parameters of an anonymous method is the anonymous-method-block.

It is an error to have a jump statement, such as goto, break, or continue, inside the anonymous method block if the target is outside the block. It is also an error to have a jump statement, such as goto, break, or continue, outside the anonymous method block if the target is inside the block.

The local variables and parameters whose scope contains an anonymous method declaration are called outer variables of the anonymous method. For example, in the following code segment, n is an outer variable:

int n = 0; Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };

Unlike local variables, the lifetime of the captured variable extends until the delegates that reference the anonymous methods are eligible for garbage collection. A reference to n is captured at the time the delegate is created.

An anonymous method cannot access the ref or out parameters of an outer scope.

No unsafe code can be accessed within the anonymous-method-block.

Anonymous methods are not allowed on the left side of the is operator.

 


Заметки

Областью действия параметров анонимного метода является блок анонимного метода.

Если целевой объект находится вне блока, то наличие оператора перехода, например goto, break или continue, в блоке анонимного метода будет ошибкой. Если целевой объект находится внутри блока, то наличие оператора перехода, например goto, break или continue, вне блока анонимного метода также будет ошибкой.

Локальные переменные и параметры, область действия которых содержит объявление анонимного метода, называются внешними переменными анонимного метода. Например, в следующем сегменте кода n является внешней переменной.

int n = 0; Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };

В отличие от локальных переменных, жизненный цикл перехваченной переменной продолжается до тех пор, пока делегаты, ссылающиеся на анонимные методы, могут выполнять сборку мусора. Ссылка на переменную n перехватывается во время создания делегата.

Анонимный метод не может обращаться к параметрам ref или out внешней области действия.

В блоке анонимного метода нельзя получить доступ ни к одному небезопасному коду.

Анонимные методы не разрешены с левой стороны оператора is.

 


Example

The following example demonstrates two ways of instantiating a delegate:

· Associating the delegate with an anonymous method.

· Associating the delegate with a named method (DoWork).

In each case, a message is displayed when the delegate is invoked.

// Declare a delegate delegate void Printer(string s);   class TestClass { static void Main() {    // Instatiate the delegate type using an anonymous method:    Printer p = delegate(string j)    {        System.Console.WriteLine(j);    };      // Results from the anonymous delegate call:    p("The delegate using the anonymous method is called.");      // The delegate instantiation using a named method "DoWork":    p = new Printer(TestClass.DoWork);      // Results from the old style delegate call:    p("The delegate using the named method is called."); }   // The method associated with the named delegate: static void DoWork(string k) {    System.Console.WriteLine(k); } }

Output

The delegate using the anonymous method is called.

The delegate using the named method is called.


Пример

В следующем примере представлено два способа создания делегата.

· Связывание делегата с анонимным методом.

· Связывание делегата с именованным методом (DoWork).

В каждом случае при вызове делегата отображается сообщение.

ß----


Overloadable Operators

C# allows user-defined types to overload operators by defining static member functions using the operator keyword. Not all operators can be overloaded, however, and others have restrictions, as listed in this table:

Operators

Overloadability

+, -, !, ~, ++, --, true, false

These unary operators can be overloaded.

+, -, *, /, %, &, |, ^, <<, >>

These binary operators can be overloaded.

==, !=, <, >, <=, >=

The comparison operators can be overloaded (but see the note that follows this table).

&&, ||

The conditional logical operators cannot be overloaded, but they are evaluated using & and |, which can be overloaded.

[]

The array indexing operator cannot be overloaded, but you can define indexers.

()

The cast operator cannot be overloaded, but you can define new conversion operators (see explicit and implicit).

+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

Assignment operators cannot be overloaded, but +=, for example, is evaluated using +, which can be overloaded.

=, ., ?:, ->, new, is, sizeof, typeof

These operators cannot be overloaded.

Note:

The comparison operators, if overloaded, must be overloaded in pairs; that is, if == is overloaded, != must also be overloaded. The reverse is also true, and similar for < and >, and for <= and >=.

       

To overload an operator on a custom class requires creating a method on the class with the correct signature. The method must be named "operator X" where X is the name or symbol of the operator being overloaded. Unary operators have one parameter, and binary operators have two parameters. In each case, one parameter must be the same type as the class or struct that declares the operator, as demonstrated in the following example:

public static Complex operator +(Complex c1, Complex c2)

For more information, see How to: Use Operator Overloading to Create a Complex Number Class.


Перегружаемые операторы

В C# пользовательские типы могут перегружать операторы путем определения функций статических членов с помощью ключевого слова operator. Не все операторы могут быть перегружены, а для некоторых операторов есть ограничения. См. следующую таблицу:

Операторы

Возможность перегрузки

+, -, !, ~, ++, --, true, false

Эти унарные операторы можно перегрузить.

+, -, *, /, %, &, |, ^, <<, >>

Эти бинарные операторы можно перегрузить.

==, !=, <, >, <=, >=

Операторы сравнения можно перегрузить (но см. примечание после таблицы).

&&, ||

Условные логические операторы нельзя перегрузить, но они вычисляются с помощью & и |, допускающих перегрузку.

[]

Оператор индексирования массива нельзя перегрузить, но можно определить индексаторы.

()

Оператор приведения нельзя перегрузить, но можно определить новые операторы преобразования (см. explicit и implicit).

+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

Операторы присвоения нельзя перегрузить, но, например, += вычисляется с помощью +, допускающего перегрузку.

=, ., ?:, ->, new, is, sizeof, typeof

Эти операторы нельзя перегрузить.

Примечание.

Операторы сравнения можно перегружать, но только парами: если перегружен оператор ==, то != также должен быть перегружен. Обратный принцип также действителен и действует для операторов < и >, а также для <= и >=.

       

Для перегрузки оператора в пользовательском классе нужно создать метод с требуемым заголовком. Метод нужно назвать "operator X", где X — имя или символ перегружаемого оператора. Унарные операторы имеют один параметр, а бинарные — два. В каждом случае один параметр должен быть такого же типа, как класс или структура, объявившие оператор, как показано в следующем примере.

public static Complex operator +(Complex c1, Complex c2)

Дополнительные сведения см. в разделе Перегрузка операторов для реализации класса комплексных чисел.


Conversion Operators

C# enables programmers to declare conversions on classes or structs so that classes or structs can be converted to and/or from other classes or structs, or basic types. Conversions are defined like operators and are named for the type to which they convert. Either the type argument to be converted, or the type of the result of the conversion, but not both, must be the containing type.


Дата добавления: 2019-03-09; просмотров: 268; Мы поможем в написании вашей работы!

Поделиться с друзьями:






Мы поможем в написании ваших работ!