添加Lesson14
This commit is contained in:
@@ -57,6 +57,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson13_事件", "Lesson13
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson13_事件练习", "Lesson13_事件练习\Lesson13_事件练习.csproj", "{AD35AAD1-3AEE-4A8D-95EB-AA3DAE7776E0}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson14_匿名函数", "Lesson14_匿名函数\Lesson14_匿名函数.csproj", "{10AB400D-490A-49B1-9ABF-3993EE0B49E0}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson14_匿名函数练习", "Lesson14_匿名函数练习\Lesson14_匿名函数练习.csproj", "{9030BA7A-FDFA-4F88-869E-918589104831}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -171,6 +175,14 @@ Global
|
||||
{AD35AAD1-3AEE-4A8D-95EB-AA3DAE7776E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AD35AAD1-3AEE-4A8D-95EB-AA3DAE7776E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AD35AAD1-3AEE-4A8D-95EB-AA3DAE7776E0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{10AB400D-490A-49B1-9ABF-3993EE0B49E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{10AB400D-490A-49B1-9ABF-3993EE0B49E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{10AB400D-490A-49B1-9ABF-3993EE0B49E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{10AB400D-490A-49B1-9ABF-3993EE0B49E0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9030BA7A-FDFA-4F88-869E-918589104831}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9030BA7A-FDFA-4F88-869E-918589104831}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9030BA7A-FDFA-4F88-869E-918589104831}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9030BA7A-FDFA-4F88-869E-918589104831}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,124 @@
|
||||
namespace Lesson14_匿名函数
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
#region 知识点一 什么是匿名函数
|
||||
//顾名思义 就是没有名字的函数
|
||||
//匿名函数的使用主要是配合委托和事件进行使用
|
||||
//脱离委托和事件 是不会使用匿名函数的
|
||||
#endregion
|
||||
|
||||
#region 知识点二 基本语法
|
||||
//delegate (参数列表)
|
||||
//{
|
||||
// //函数体
|
||||
// return 返回值;
|
||||
//};
|
||||
//何时使用?
|
||||
//1.函数中传递委托参数时
|
||||
//2.委托或事件赋值时
|
||||
#endregion
|
||||
|
||||
#region 知识点三 使用
|
||||
//注意:无法直接在函数中定义匿名函数,必须使用委托变量来接收匿名函数
|
||||
//1.无参无返回
|
||||
Action a = delegate ()//用一个无参无返回的委托变量 来接收匿名函数
|
||||
{
|
||||
Console.WriteLine("无参无返回匿名函数逻辑");
|
||||
};
|
||||
a(); //调用匿名函数
|
||||
|
||||
|
||||
//2.有参
|
||||
Action<int, string> a2 = delegate (int a, string b)
|
||||
{
|
||||
Console.WriteLine($"有参无返回的匿名函数逻辑,参数a:{a},参数b:{b}");
|
||||
};
|
||||
a2(10, "hello");
|
||||
|
||||
|
||||
//3.有返回值
|
||||
Func<int> f = delegate ()
|
||||
{
|
||||
Console.WriteLine("无参有返回值的匿名函数逻辑");
|
||||
return 10;
|
||||
};
|
||||
Console.WriteLine(f());
|
||||
|
||||
|
||||
//4.一般情况会作为函数参数传递 或者 作为函数返回值
|
||||
Test t = new Test();
|
||||
|
||||
Action ac = delegate ()
|
||||
{
|
||||
Console.WriteLine("作为类成员变量的匿名函数逻辑");
|
||||
};
|
||||
|
||||
|
||||
|
||||
//参数传递
|
||||
t.Dosomething(100, delegate()
|
||||
{
|
||||
Console.WriteLine("作为参数传递的匿名函数逻辑");
|
||||
});
|
||||
t.Dosomething(200, ac);
|
||||
|
||||
|
||||
|
||||
|
||||
//返回值
|
||||
Action ac2 = t.GetFun();//可以先接收返回的匿名函数
|
||||
ac2();//调用函数
|
||||
|
||||
//也可以直接调用返回的匿名函数
|
||||
t.GetFun()();
|
||||
#endregion
|
||||
|
||||
#region 知识点四 匿名函数的缺点
|
||||
//添加到委托或事件中的匿名函数 无法被移除
|
||||
Action ac3 = delegate ()
|
||||
{
|
||||
Console.WriteLine("匿名函数1");
|
||||
};
|
||||
|
||||
ac3 += delegate ()
|
||||
{
|
||||
Console.WriteLine("匿名函数2");
|
||||
};
|
||||
ac3();//调用时 会依次调用两个匿名函数
|
||||
//因为匿名函数没有名字 所以无法通过名字来移除
|
||||
//ac3 -= ???
|
||||
//所以只能通过清空委托变量来移除
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
class Test
|
||||
{
|
||||
public Action action;
|
||||
|
||||
|
||||
//作为函数参数传递匿名函数
|
||||
public void Dosomething(int a,Action fun)
|
||||
{
|
||||
Console.WriteLine(a);
|
||||
fun();
|
||||
}
|
||||
|
||||
//作为函数返回值返回匿名函数
|
||||
public Action GetFun()
|
||||
{
|
||||
return delegate ()
|
||||
{
|
||||
Console.WriteLine("作为返回值 函数内部返回的函数逻辑");
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 总结
|
||||
// 匿名函数 就是没有名字的函数
|
||||
// 固定写法//delegate (参数列表){}
|
||||
// 主要是在 委托传递和存储时 为了方便可以直接使用倪敏该函数
|
||||
// 缺点是 没有办法指定移除
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace Lesson14_匿名函数练习
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Func<int, int> func = Dosomething(3);
|
||||
int result = func(2);
|
||||
Console.WriteLine(result);
|
||||
}
|
||||
public static Func<int, int> Dosomething(int value)
|
||||
{
|
||||
return delegate (int x)
|
||||
{
|
||||
return value * x;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user