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

C#之Lambda学习一

时间:2016-05-07 09:36:31      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace DelegateDemo1
{
    class Program
    {
        static void One()
        {
            Console.WriteLine("one");
            throw new Exception("error in one");
        }
        static void Two()
        {
            Console.WriteLine("Two");
        }

        static void Main(string[] args)
        {

            string mid = ",middle part,";
            /*Func<string, string>:接受一个字符串参数,返回一个字符串 在这个匿名方法中不能使用
             break,goto,continue跳到函数外部,也不能够跳到内部
             */
            Func<string, string> anonDel = delegate(string param)
            {
                param += mid;
                param += "and this is end";
                return param;
            };
            Console.WriteLine(anonDel("start"));

            /*lambda =>:左边列出了所需要的参数(param)
              lambda => :右边定义了赋予lambda变量的方法实现的代码
             */
            Func<string, string> lambda = param =>
            {
                param += mid;
                param += "and this is end";
                return param;
            };
            Console.WriteLine(anonDel("lambda"));


            //lambda一个参数  编译器会添加一条隐式return语句
            Func<string, string> oneParam = s => string.Format("change uppercase {0}", s.ToUpper());
            //添加显示的return语句
            Func<string, string> oneParamR = s =>
            {
                return string.Format("change uppercase {0}", s.ToUpper());
            };

            Console.WriteLine("oneParam is " + oneParam("Test"));

            //lambda两个参数  编译器会添加一条隐式return语句
            Func<double, double, double> towParams = (x, y) => x * y;
            //添加显示的return语句
            Func<double, double, double> towParamsR = (x, y) =>
            {
                return x * y;
            };
            Console.WriteLine("towParams is " + towParams(3,2));
            Console.ReadKey();
        }
    }
}

C#之Lambda学习一

标签:

原文地址:http://blog.csdn.net/rose_girls/article/details/51332943

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