c#有返回值的委托,有Action吗
时间: 2024-03-02 10:51:23 浏览: 27
`Action`是C#中的一个预定义委托,它表示不带返回值的方法。其定义格式如下:
```
public delegate void Action();
```
其中,`void`表示该委托不带返回值,`Action`为委托名,`()`表示该委托不带参数。
如果需要带参数的`Action`委托,可以使用`Action<>`泛型委托,例如:
```
public delegate void Action<T>(T obj);
```
其中,`<T>`表示泛型参数,`T obj`表示该委托带有一个参数。
使用`Action`委托时,只需要定义一个符合委托签名的方法,然后将该方法赋值给委托即可。示例如下:
```
void PrintMessage()
{
Console.WriteLine("Hello, world!");
}
Action printAction = PrintMessage;
printAction(); // 输出 "Hello, world!"
```
在上面的示例中,定义了一个不带参数的`Action`委托`printAction`,然后将`PrintMessage`方法赋值给该委托,并调用该委托,得到输出"Hello, world!"。
相关问题
C# action委托使用
C#中的Action委托是一个预定义的委托类型,它可以引用一个没有返回值的方法。Action委托可以用于指向不同参数类型和不同参数个数的方法。
以下是使用Action委托的几个示例:
1. 指向没有参数的方法:
```csharp
static void PrintString()
{
Console.WriteLine("hello world");
}
static void Main()
{
Action a = PrintString;
a(); // 输出:hello world
}
```
2. 指向有一个int类型参数的方法:
```csharp
static void PrintNumber(int num)
{
Console.WriteLine("The number is: " + num);
}
static void Main()
{
Action<int> a = PrintNumber;
a(10); // 输出:The number is: 10
}
```
3. 指向有一个string类型参数的方法:
```csharp
static void PrintMessage(string message)
{
Console.WriteLine("The message is: " + message);
}
static void Main()
{
Action<string> a = PrintMessage;
a("Hello"); // 输出:The message is: Hello
}
```
4. 指向有一个int类型参数和一个string类型参数的方法:
```csharp
static void PrintInfo(int num, string message)
{
Console.WriteLine("The number is: " + num);
Console.WriteLine("The message is: " + message);
}
static void Main()
{
Action<int, string> a = PrintInfo;
a(10, "Hello"); // 输出:The number is: 10 The message is: Hello
}
```
需要注意的是,Action委托没有返回值,但是可以通过在Action后面声明泛型来指定方法的参数类型。方法参数类型和Action后面声明的委托类型是要对应的。
c# lambda 委托
C#中的Lambda表达式是一种方便且简洁的方式,用于创建委托或表达式树类型。通过Lambda表达式,您可以编写内联的代码块,而不必将它们编写为单独的方法或匿名方法。Lambda表达式通常使用委托类型来声明,这些委托类型定义了要执行的代码块的输入和输出参数。
使用Lambda表达式,您可以在C#中轻松地创建委托。Lambda表达式的语法如下:
(parameter_list) => expression
其中,parameter_list是逗号分隔的参数列表,expression是返回值。
例如,下面是一个将两个整数相加的Lambda表达式:
(int x, int y) => x + y
您可以将其用作委托参数,例如:
Action<int, int> add = (x, y) => Console.WriteLine(x + y);
这个Lambda表达式被转换成一个委托类型Action<int, int>,并赋值给add变量。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)