Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 4 章 高级 C# 高级 C#

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在本章中,我们将介绍以第 2章和第3 章中探讨的概念为基础的高级 C# 主题。你应该按顺序阅读前四节,其余各节可以任意顺序阅读。

与会代表

委托是一个知道如何调用方法的对象。

委托类型定义委托实例可调用的方法类型。具体来说,它定义了方法的返回类型参数类型。下面定义了一个名为Transformer 的委托类型:

delegate int Transformer (int x);

Transformer 兼容任何返回类型为 、参数为 的方法,例如 this:int int

int Square (int x) { return x * x; }

或者说得更直白一些:

int Square (int x) => x * x;

将方法分配给委托变量会创建一个委托实例

Transformer t = Square;

调用委托实例的方式与调用方法相同:

int answer = t(3);    // answer is 9

下面是一个完整的例子:

Transformer t = Square;          // Create delegate instance
int result = t(3);               // Invoke delegate
Console.WriteLine (result);      // 9

int Square (int x) => x * x;

delegate int Transformer (int x);   // Delegate type declaration

委托实例实际上是调用者的委托:调用者调用委托,然后委托调用目标方法。这种间接方式将调用者与目标方法分离开来。

声明

Transformer t = Square;

的简称:

Transformer t = new Transformer (Square);
备注

从技术上讲,当我们在不带括号或参数的情况下引用Square 时,我们是在指定一个方法组。如果方法是重载的,C# 将根据分配给它的委托的签名选择正确的重载。

表达方式

t(3)

t.Invoke(3)
备注

委托类似于回调,回调是一个通用术语,用于捕捉 C 函数指针等构造。

使用代理编写插件方法

委托变量在运行时被分配给一个方法。这对于编写插件方法非常有用。在本例中,我们有一个名为Transform 的实用程序方法,该方法可对整数数组中的每个元素进行变换。Transform 方法有一个委托参数,可用于指定插件变换:

int[] values = { 1, 2, 3 };
Transform (values, Square);      // Hook in the Square method

foreach (int i in values)
  Console.Write (i + "  ");      // 1   4   9

void Transform (int[] values, Transformer t)
{
  for (int i = 0; i < values.Length; i++)
    values[i] = t (values[i]);
}

int Square (int x) => x * x;
int Cube (int x) => x * x * x;

delegate int Transformer (int x);

我们只需将第二行代码中的Square 改为Cube ,就可以改变转换。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038