Skip to content

Commit 03e52c1

Browse files
doc: update docs/cs.md (#926)
1 parent e544b8f commit 03e52c1

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed

docs/cs.md

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,212 @@ var result = students
13201320
```cs
13211321
["Charlie","Damon","David"]
13221322
```
1323+
事件和委托
1324+
----
1325+
1326+
> 在 .NET 中委托提供后期绑定机制。 后期绑定意味着调用方在你所创建的算法中至少提供一个方法来实现算法的一部分, 而不是在创建委托时提供完整的算法。 委托的这种特性使得 .NET 成为一种灵活、可扩展的编程环境。
1327+
1328+
### 定义委托类型
1329+
1330+
```cs
1331+
// 使用 delegate 关键字定义委托
1332+
public delegate void MyDelegate(int x, string y);
1333+
1334+
// 上述委托对应的函数实现应该类似:
1335+
public void MyMethod(int x, string y);
1336+
```
1337+
1338+
### 创建委托实例
1339+
1340+
```cs
1341+
// 创建委托实例
1342+
MyDelegate myDelegate = new MyDelegate(MyMethod);
1343+
```
1344+
1345+
### 调用委托
1346+
1347+
```cs
1348+
// 调用委托,传入对应类型的参数
1349+
myDelegate(10, "Hello");
1350+
```
1351+
1352+
### 委托作为参数
1353+
<!--rehype:wrap-class=col-span-2-->
1354+
1355+
```cs
1356+
// 定义另一个委托类型
1357+
public delegate int MyDelegate2(int x, int y);
1358+
1359+
// 定义一个方法,接收委托作为参数
1360+
public int MyMethod2(int x, int y, MyDelegate2 myDelegate)
1361+
{
1362+
return myDelegate(x, y);
1363+
}
1364+
1365+
// 创建委托实例
1366+
MyDelegate2 myDelegate2 = new MyDelegate2(Add);
1367+
1368+
// 调用 MyMethod2 方法,并传入委托作为参数
1369+
int result = MyMethod2(10, 20, myDelegate2);
1370+
```
1371+
1372+
### 多播委托
1373+
<!--rehype:wrap-class=col-span-3-->
1374+
1375+
> 我们预先提供这些可用的方法
1376+
1377+
```cs
1378+
public void Sub(int x, int y)
1379+
{
1380+
Console.WriteLine("x-y=" + (x - y));
1381+
}
1382+
1383+
public void Mul(int x, int y)
1384+
{
1385+
Console.WriteLine("x*y=" + (x * y));
1386+
}
1387+
```
1388+
1389+
```cs
1390+
// 定义一个委托类型
1391+
public delegate void MyDelegate3(int x, int y);
1392+
// 定义一个方法,接收委托作为参数
1393+
public void MyMethod3(int x, int y, MyDelegate3 myDelegate)
1394+
{
1395+
myDelegate(x, y);
1396+
}
1397+
1398+
// 定义另一个委托类型
1399+
public delegate void MyDelegate4(int x, int y);
1400+
// 定义一个方法,接收委托作为参数
1401+
public void MyMethod4(int x, int y, MyDelegate4 myDelegate)
1402+
{
1403+
myDelegate(x, y);
1404+
}
1405+
1406+
// 定义一个方法,接收委托作为参数
1407+
public void MyMethod5(int x, int y, MyDelegate3 myDelegate, MyDelegate4 myDelegate2)
1408+
{
1409+
myDelegate(x, y);
1410+
myDelegate2(x, y);
1411+
}
1412+
1413+
// 多播委托
1414+
MyDelegate3 myDelegate31 = new MyDelegate3(Sub);
1415+
MyDelegate4 myDelegate41 = new MyDelegate4(Mul);
1416+
1417+
// 调用 MyMethod3 方法,并传入委托作为参数
1418+
// output:
1419+
// x-y=5
1420+
MyMethod3(10, 5, myDelegate31);
1421+
1422+
// 调用 MyMethod4 方法,并传入委托作为参数
1423+
// output:
1424+
// x*y=50
1425+
MyMethod4(10, 5, myDelegate41);
1426+
1427+
// 调用 MyMethod5 方法,并传入委托作为参数
1428+
// output:
1429+
// x-y=5
1430+
// x*y=50
1431+
MyMethod5(10, 5, myDelegate31, myDelegate41);
1432+
```
1433+
1434+
### Action 委托
1435+
1436+
> Action 委托的变体可包含最多16个参数,返回类型为 `void`
1437+
1438+
```cs
1439+
// 创建一个Action
1440+
public Action<int, string> myAction;
1441+
1442+
// 给Action赋值
1443+
myAction = (x, y) => Console.WriteLine("x+y=" + (x + y));
1444+
1445+
// 直接调用Action
1446+
myAction(10, "Hello");
1447+
1448+
// 使用 null 合并运算符调用Action
1449+
myAction?.Invoke(10, "Hello");
1450+
```
1451+
1452+
### Func 委托
1453+
1454+
> Func 委托的变体可包含最多16个参数,返回类型可以是任意类型 T
1455+
1456+
```cs
1457+
// 创建一个Func,最后一个参数是返回类型
1458+
public Func<int, int, string> myFunc;
1459+
1460+
// 给Func赋值
1461+
myFunc = (x, y) => "x+y=" + (x + y);
1462+
1463+
// 调用Func
1464+
string result = myFunc(10, 20);
1465+
1466+
// 使用 null 合并运算符调用Func
1467+
string result2 = myFunc?.Invoke(10, 20);
1468+
```
1469+
1470+
> 和委托类似,事件是后期绑定机制。 实际上,事件是建立在对委托的语言支持之上的。事件是c#内部对委托的一种封装,它提供一种更加面向对象的编程模型和观察者模式的实现。
1471+
1472+
### 事件定义
1473+
1474+
```cs
1475+
// 使用 event 关键字定义事件
1476+
public event EventHandler<EventArgs> MyEvent;
1477+
```
1478+
1479+
### 事件订阅
1480+
1481+
> 我们实现定义一个方法作为事件处理器,并订阅事件
1482+
1483+
```cs
1484+
public void MyEventHandler(object sender, EventArgs e)
1485+
{
1486+
// 事件处理逻辑
1487+
// ...
1488+
}
1489+
```
1490+
1491+
```cs
1492+
// 订阅事件
1493+
MyEvent += MyEventHandler;
1494+
1495+
// 取消订阅事件
1496+
MyEvent -= MyEventHandler;
1497+
```
1498+
1499+
### 事件触发
1500+
1501+
```cs
1502+
// 触发事件
1503+
MyEvent?.Invoke(this, new EventArgs());
1504+
```
1505+
1506+
### 事件参数
1507+
1508+
<!--rehype:wrap-class=col-span-2-->
1509+
1510+
```cs
1511+
// 定义事件参数
1512+
public class MyEventArgs : EventArgs
1513+
{
1514+
public int Value { get; set; }
1515+
}
1516+
1517+
// 新的Handler
1518+
public void MyEventHandler2(object sender, MyEventArgs e)
1519+
{
1520+
// 事件处理逻辑,这里可以获取到事件参数
1521+
Console.WriteLine("事件参数的值:" + e.Value);
1522+
}
1523+
1524+
// 触发事件
1525+
// output:
1526+
// 事件参数的值:10
1527+
MyEvent?.Invoke(this, new MyEventArgs { Value = 10 });
1528+
```
13231529

13241530
语法糖
13251531
----

0 commit comments

Comments
 (0)