首页  >  文章  >  后端开发  >  C# 匿名函数

C# 匿名函数

PHPz
PHPz原创
2024-09-03 15:14:47342浏览

C#中没有名称的函数称为匿名函数,也可以表示为没有名称的函数。 C# 中的匿名函数有两种类型,即 C# 中的 Lambda 表达式和 C# 中的匿名方法,其中用于创建委托的匿名函数在 C# 中称为 Lambda 表达式,使用该表达式可以创建本地函数并作为参数和查询传递LINQ 也可以借助 Lambda 表达式来编写。匿名方法也提供相同的功能,只不过它允许不使用参数列表。

C# 中的匿名函数类型

C# 中有两种类型的匿名函数。他们是:

1. Lambda 表达式

  • 可以使用一种称为 Lambda 表达式的匿名函数来创建委托。
  • 可以使用 Lambda 表达式创建本地函数,并可以将其作为参数传递。
  • 可以借助 Lambda 表达式编写 LINQ 的查询。
  • C#中lambda表达式的语法如下:
(input-parameters) => expression
示例#1

在程序中演示 Lambda 表达式求数字平方的 C# 程序:

代码:

using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
delegate int Findsquare(int number);
//main method is called
static void Main(string[] args)
{
//a lambda expression to find the square of a number is defined
Findsquare Obtainsquare = r => r * r;
int l = Obtainsquare(3);
Console.WriteLine("The Square of the given number is: "+l);
}
}
}

输出:

C# 匿名函数

在上面的程序中,定义了一个名为program的命名空间。然后定义一个名为check的类。然后调用main方法。然后定义 lambda 表达式来求数字的平方。显示使用 lambda 表达式找到的数字的平方。

示例#2

在程序中演示 Lambda 表达式的 C# 程序,以确定列表中是否存在数字:

代码:

using System;
using System.Collections.Generic;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//a new list of type integer is created
List<int> mylist = new List<int>();
//integers are added to the list using add() method
mylist.Add(10);
mylist.Add(20);
mylist.Add(30);
//a lambda expression and find method is used to determine if a number is present in the list
int found = mylist.Find(r => r == 20);
if(found==0)
{
Console.WriteLine("The number is present in the list");
}
else
{
Console.WriteLine("The number is not present in the list");
}
//a lambda expression and find method is used to determine if a number is present in the list
found = mylist.Find(r => r == 40);
if(found==0)
{
Console.WriteLine("The number is present in the list");
}
else
{
Console.WriteLine("The number is not present in the list");
}
}
}
}

输出:

C# 匿名函数

在上面的程序中,定义了一个名为program的命名空间。然后定义一个名为check的类。然后调用main方法。然后创建一个新的整数类型列表。然后使用 Add() 方法将整数添加到列表中。然后使用 lambda 表达式和 Find() 方法来确定列表中是否存在数字。

2.匿名方法

  • C# 中没有名称的方法称为匿名方法。
  • 匿名方法是在 C# 2.0 版本中引入的。
  • 当要创建内联方法时,我们会使用匿名方法,并且参数也必须传递给该方法,类似于我们将参数传递给任何其他方法的方式。
  • 关键字 delegate 用于定义匿名方法,该方法可以赋值给 delegate 类型的变量。
  • C#中匿名方法的语法如下:
Delegate(parameter_list)
{
//Block of code
};
示例#1

在程序中演示匿名方法的 C# 程序:

代码:

using System;
//a class called program is defined
class program
{
//a delegate is created by using delegate keyword
public delegate void subject(string favsubject);
// Main method is called
static public void Main()
{
// a parameter is passed to the anonymous method using delegate keyword
subject sub = delegate(string favsubject)
{
Console.WriteLine("{0} is my favourite subject", favsubject);
};
sub("C#");
}
}

输出:

C# 匿名函数

在上面的程序中,定义了一个类程序。然后使用 delegate 关键字创建委托。然后调用main方法。然后使用 delegate 关键字将参数传递给匿名方法。

示例#2

C# 程序演示程序中的匿名方法可以访问外部方法中定义的变量:

代码:

using System;
//a class called program is defined
class program
{
//anonymous method is declared using delegate keyword
public delegate void subject(string favsubject);
// Main method is called
static public void Main()
{
//a string variable is defined in the outside method from anonymous method
string favsubject1 = "Coding_in_C#";
// a parameter is passed to the anonymous method using delegate keyword
subject sub = delegate(string favsubject)
{
Console.WriteLine("{0} is my favourite subject", favsubject);
Console.WriteLine("I also like {0}", favsubject1);
};
sub("C#");
}
}

输出:

C# 匿名函数

在上面的程序中,定义了一个名为program的类。然后使用 delegate 关键字创建委托。然后调用main方法。然后定义一个字符串变量,它是匿名方法的外部方法。然后使用 delegate 关键字将参数传递给匿名方法。匿名方法也可以访问匿名方法之外的变量。

以上是C# 匿名函数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn