c#隐藏函数 lambda表达式 泛型综合使用代码

本文介绍了C#中委托的基本用法,包括通过不同方式创建委托实例,并演示了从C#2.0的匿名方法到C#3.0 Lambda表达式的演进。此外,还展示了如何使用Lambda表达式进行简单的函数式编程。
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);
        }

    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值