using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NimingHanshu
{
class Program
{
delegate void TestDelegate(string s);
delegate int del(int i);
//TResult 是返回值,Targ0是参数值
delegate TResult Func<Targ0, TResult>(Targ0 arg0);
static void M(string s)
{
Console.WriteLine(s);
}
static void Main(string[] args)
{
//DelegateHistory();
//StartTread();
Console.ReadLine();
}
private static void DelegateHistory()
{
TestDelegate testDelA = new TestDelegate(M);
//C#2.0 匿名函数
TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };
//C#3.0 Lambda表达式
TestDelegate testDelC = (x) => { Console.WriteLine(x); };
testDelA("this is a deletegate");
testDelB("this is a anonymous method");
testDelC("this is a lambda expression");
}
//匿名方法的使用范例
//匿名方法不能使用 ref out 作为参数列表
private static void StartTread()
{
System.Threading.Thread t1 = new System.Threading.Thread
(
delegate()
{
Console.WriteLine("Hello ");
Console.WriteLine(" World");
}
);
t1.Start();
}
//Lambda表达式
//语法要求 ()=>expression
private static void Lambda()
{
del mydel = x => x * x;
Console.WriteLine(mydel(5));
Func<int, bool> myFunc = x => x == 5;
Console.WriteLine(myFunc);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NimingHanshu
{
class Program
{
delegate void TestDelegate(string s);
delegate int del(int i);
//TResult 是返回值,Targ0是参数值
delegate TResult Func<Targ0, TResult>(Targ0 arg0);
static void M(string s)
{
Console.WriteLine(s);
}
static void Main(string[] args)
{
//DelegateHistory();
//StartTread();
Console.ReadLine();
}
private static void DelegateHistory()
{
TestDelegate testDelA = new TestDelegate(M);
//C#2.0 匿名函数
TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };
//C#3.0 Lambda表达式
TestDelegate testDelC = (x) => { Console.WriteLine(x); };
testDelA("this is a deletegate");
testDelB("this is a anonymous method");
testDelC("this is a lambda expression");
}
//匿名方法的使用范例
//匿名方法不能使用 ref out 作为参数列表
private static void StartTread()
{
System.Threading.Thread t1 = new System.Threading.Thread
(
delegate()
{
Console.WriteLine("Hello ");
Console.WriteLine(" World");
}
);
t1.Start();
}
//Lambda表达式
//语法要求 ()=>expression
private static void Lambda()
{
del mydel = x => x * x;
Console.WriteLine(mydel(5));
Func<int, bool> myFunc = x => x == 5;
Console.WriteLine(myFunc);
}
}
}
本文介绍了C#中委托的基本用法,包括通过不同方式创建委托实例,并演示了从C#2.0的匿名方法到C#3.0 Lambda表达式的演进。此外,还展示了如何使用Lambda表达式进行简单的函数式编程。
1195

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



