search
HomeBackend DevelopmentC#.Net TutorialC# 2.0 Specification (anonymous method) (2)

21.7 Delegate instance equality

The following rules apply to results produced by the equality operator (§7.9.8) and the object.Equals method of anonymous method delegate instances.
l When delegate instances result from evaluation of semantically identical anonymous method expressions with the same set of captured external variables, they are said to be equal (but are not required to be).
l When delegate instances are represented by anonymous method expressions with different semantics, or with different sets of captured external variables, they are never equal.

21.8 Explicit assignment

The explicit assignment status of anonymous method parameters is the same as that of named methods. That is, reference parameters and value parameters are explicitly assigned initial values, while output parameters do not need to be assigned initial values. Also, output parameters must be explicitly assigned before the anonymous method returns normally (§5.1.6).
When control is transferred to the program block of the anonymous method expression, the explicit assignment state of the external variable v is the same as the explicit assignment state of v before the anonymous method expression. That is, explicit assignments to external variables will be inherited from the anonymous method expression context. Within anonymous method blocks, explicit assignments are deduced as within ordinary blocks (§5.3.3).
The explicit assignment status of variable v after the anonymous method expression is the same as its explicit assignment status before the anonymous method expression.

For example,

delegate bool Filter(int i);
void F() {
int max;
// 错误,max没有明确赋值
Filter f = delegate(int n) { return n < max; }
max = 5;
DoWork(f);
}

will generate a compile-time error because max is not explicitly assigned where the anonymous method is declared. Example

delegate void D();
void F() {
int n;
D d = delegate { n = 1; };
d();
//错误,n没有明确赋值
Console.WriteLine(n);
}

will also generate a compile-time error, because the assignment of n inside the anonymous method has no effect on the explicit assignment state of n outside the anonymous method.

21.9 Method Group Conversion

Similar to the implicit anonymous method conversion described in §21.3, there is also an implicit conversion from a method group (§7.1) to a compatible delegate type Convert.
For a given method group E and delegate type D, if delegate creation expressions of the form new D(E) are allowed (§7.5.10.3 and §20.9.6), then there is an implicit path from E to D Conversion, and the result of the conversion is exactly equivalent to new D(E).
In the following example

using System;
using System.Windows.Forms;
class AlertDialog
{
Label message = new Label();
Button okButton = new Button();
Button cancelButton = new Button();`
public AlertDialog() {
okButton.Click += new EventHandler(OkClick);
cancelButton.Click += new EventHandler(CancelClick);
...
}
void OkClick(object sender, EventArgs e) {
...
}
void CancelClick(object sender, EventArgs e) {
...
}
}

the constructor uses new to create two delegate instances. Implicit method group conversions allow this to be simplified to

public AlertDialog() {
okButton.Click += OkClick;
cancelButton.Click += CancelClick;
...
}

As with all other implicit and explicit conversions, conversion operators can be used to explicitly perform a specific conversion. For this purpose, the example

object obj = new EventHandler(myDialog.OkClick);

can be replaced as follows.

object obj = (EventHandler)myDialog.OkClick;

Method combination anonymous method expressions can affect overload resolution (overload resolution), but they do not participate in type inference. See §20.6.4 for more details.

21.10 Implementation Example

This section describes the possible implementation of anonymous methods in the form of standard C# components. The implementation described here is based on the same principles employed by the Microsoft C# compiler, but it is by no means mandatory or the only possible implementation.
The latter part of this section gives several example codes, which contain anonymous methods with different characteristics. For each example, we'll provide a corresponding transformation of the code using a unique standard C# construct. In these examples, the identifier D is assumed to represent the following delegate type.

public delegate void D();

The simplest form of an anonymous method is the one that does not capture external variables.

class Test
{
static void F() {
D d = delegate { Console.WriteLine("test"); };
}
}

This code can be converted to a delegate instance that references a static method generated by the compiler, and the code of the anonymous method will be placed into the static method. ,

class Test
{
static void F() {
D d = new D(__Method1);
}
static void __Method1() {
Console.WriteLine("test");
}
}

In the following example, the anonymous method references the instance member of this.

class Test
{
int x;
void F() {
D d = delegate { Console.WriteLine(x); };
}
}

This can be converted to an instance method generated by the compiler that contains anonymous method code.

class Test
{
int x;
void F() {
D d = new D(__Method1);
}
void __Method1() {
Console.WriteLine(x);
}
}

In this example, the anonymous method captures a local variable.


class Test
{
void F() {
int y = 123;
D d = delegate { Console.WriteLine(y); };
}
}

该局部变量的生存期现在至少必须延长到匿名方法委托的生存期为止。这可以通过将局部变量“提升(lifting)”为编译器生成的(compiler-generated)类的字段来完成。局部变量的实例化对应于创建一个编译器生成的类的实例,而访问局部变量将对应于访问编译器生成的类实例的一个字段。并且,匿名方法将成为编译器生成类的实例方法。

class Test
{
void F() {
__locals1 = new __Locals1();
__locals1.y = 123;
D d = new D(__locals1.__Method1);
}
class __Locals1
{
public int y;
public void __Method1() {
Console.WriteLine(y);
}
}
}

最后,如下匿名方法将捕获this,以及具有不同生存期的两个局部变量。

class Test
{
int x;
void F() {
int y = 123;
for (int i = 0; i < 10; i++) {
int z = i * 2;
D d = delegate { Console.WriteLine(x + y + z); };
}
}
}

在这里,编译器将为每个语句块生成类,在这些语句块中局部变量将被捕获,而在不同块中的局部变量将会有独立的生存期。

__Locals2的实例,编译器为内部语句块生成的类,包含局部变量z和引用__Locals1实例的字段。__Locals1的实例,编译器为外部语句块生成的类,包含局部变量y和引用封闭函数成员的this的字段。通过这些数据结构,你可以通过__Locals2的一个实例到达所有被捕获的局部变量,并且匿名方法的代码可以作为那个类的实例方法而实现。

class Test
{
void F() {
__locals1 = new __Locals1();
__locals1.__this = this;
__locals1.y = 123;
for (int i = 0; i < 10; i++) {
__locals2 = new __Locals2();
__locals2.__locals1 = __locals1;
__locals2.z = i * 2;
D d = new D(__locals2.__Method1);
}
}
class __Locals1
{
public Test __this;
public int y;
}
class __Locals2
{
public __Locals1 __locals1;
public int z;
public void __Method1() {
Console.WriteLine(__locals1.__this.x + __locals1.y + z);
}
}
}

(匿名方法完)


以上就是C# 2.0 Specification(匿名方法)(二)的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools