c#隐藏函数 lambda表达式 泛型综合使用代码
2017-06-13 22:23
330 查看
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);
}
}
}
相关文章推荐
- Java 8 之 lambda 表达式简单使用入门实例代码
- 编写高质量代码改善C#程序的157个建议[为泛型指定初始值、使用委托声明、使用Lambda替代方法和匿名方法]
- android studio 使用retrolambda库 让代码支持lambda 表达式
- 编写高质量代码改善C#程序的157个建议——建议37:使用Lambda表达式代替方法和匿名方法
- 编写高质量代码改善C#程序的157个建议[为泛型指定初始值、使用委托声明、使用Lambda替代方法和匿名方法]
- Java 8 之 lambda 表达式简单使用入门实例代码。
- 泛型、函数式接口基础复习以及Lambda表达式、Optional的使用
- Lambda表达式, 可以让我们的代码更优雅.
- 使用VS正则表达式查找替换,批量修改Reflactor生成的代码问题
- [导入][原创]使用正则表达式找出HTML代码内所有IMG图片的SRC链接地址
- HessianCSharp 1.3.3版本前使用泛型以及接口interface继承问题解决方案!!(实例代码)
- 在C++中使用Lambda函数提高代码性能
- 在C++中使用Lambda函数提高代码性能
- 使用正则表达式优化网页代码
- 匿名方法(暨 lambda 表达式)使用指南
- 【转】在CS代码中使用正则表达式验证!
- 一个使用TBB Lambda 表达式实现并行执行的例子(转)
- 使用正则表达式优化网页代码
- 使用Action、Func和Lambda表达式
- 使用泛型程序简化代码设计