您的位置:首页 > 编程语言 > C#

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);

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息