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

来源:互联网 发布:淘宝展现关键词是什么 编辑:程序博客网 时间:2024/06/05 00:35
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);
        }

    }
}

原创粉丝点击