码迷,mamicode.com
首页 > Windows程序 > 详细

C#匿名函数

时间:2015-05-21 07:50:30      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Anonymous
{
    class Program
    {
        delegate void TestDelegate(string s);
        delegate int del(int i);
        delegate TResult Func<TArg0,TResult>(TArg0 arg0);
        static void M(string s)
        {
            Console.WriteLine(s);
        }
        static void Main(string[] args)
        {
            DelegateHistory();

            startThread();
            Lambda();
            Console.ReadLine();
        }
        private static void DelegateHistory()
        {
            TestDelegate testDelA = new TestDelegate(M);
            //C#2.0 Anonymous Method
            TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };
            //c#3.0 Lambda Expression
            TestDelegate testDelC = (x) => { Console.WriteLine(x); };
            testDelA("Hello,this is a delegate");
            testDelB("This is a anonymous method");
            testDelC("this is a lambda expression");
         }
        private static void startThread()
        {
            System.Threading.Thread t1 = new System.Threading.Thread
            (delegate()
            {
                Console.Write("Hello");
                Console.WriteLine("World!");
            }

            );
            t1.Start();

        }
        private static void Lambda()
        { 
            //()=>expression
            del myDelegate = x => x * x;
            Console.WriteLine(myDelegate(5));
            Func<int,bool> myFunc=x=>x==5;
            Console.WriteLine(myFunc(4));
        }


    }
}

 

C#匿名函数

标签:

原文地址:http://www.cnblogs.com/heisaijuzhen/p/4518734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!