“`
“`代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 拉姆拉表达式
{
/// <summary>
/// C# 匿名方法和拉姆达表达式
/// </summary>
class Program
{
public delegate void myDel();//【1】
public delegate void myDelString (string str);//【2】
public delegate string myDelReString(string str);//【3】
public delegate int myDelReInt(int num1,int num2);//【4】
static void Main(string[] args)
{
////【1】--------------------------------------------------
myDel dlg = new myDel(todo);
myDel dlgA = todo;
//匿名函数定义
myDel dlgB = delegate() { Console.WriteLine("方法B"); };
//拉姆达
myDel dlgC = ()=> { Console.WriteLine("方法C"); };
////【1】--------------------------------------------------
////【2】--------------------------------------------------
myDelString dlgD = s => Console.WriteLine(s);
dlgD("拉达姆");
////【2】--------------------------------------------------
////【3】--------------------------------------------------
myDelReString dlgE = s => s + "+拉达姆";
string str=dlgE("拉达姆");
Console.WriteLine(str);
////【3】--------------------------------------------------
////【4】--------------------------------------------------
toAdd((a, b) => a + b);
////【4】--------------------------------------------------
////【SSS】--------------------------------------------------
List<int> list = new List<int>() { 1, 3, 5, 7, 9 };
IEnumerable<int> nums= list.Where(w => w > 5);
foreach (var item in nums)
{
Console.WriteLine(item);
}
////【SSS】--------------------------------------------------
Console.Read();
}
public static void todo()
{
Console.WriteLine("方法");
}
public static void toAdd(myDelReInt delInt)
{
int result = delInt(1,2);
Console.WriteLine(result);
}
}
}
本文深入探讨了C#编程语言中匿名方法和拉姆达表达式的使用,通过实例展示了如何定义、调用以及实现复杂操作。

3842

被折叠的 条评论
为什么被折叠?



