C# Delegate ,Anonymous methods,lambda expression

using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        delegate string UppercaseDelegate(string input);
 
        static string UppercaseFirst(string input)
        {
            char[] buffer = input.ToCharArray();
            buffer[0] = char.ToUpper(buffer[0]);
            return new string(buffer);
        }
 
        static string UppercaseLast(string input)
        {
            char[] buffer = input.ToCharArray();
            buffer[buffer.Length - 1] = char.ToUpper(buffer[buffer.Length - 1]);
            return new string(buffer);
        }
 
        static string UppercaseAll(string input)
        {
            return input.ToUpper();
        }
 
        static void WriteOutput(string input, UppercaseDelegate del)
        {
            Console.WriteLine("Your string before: {0}", input);
            Console.WriteLine("Your string after: {0}", del(input));
        }
 
 
        static void Main(string[] args)
        {
            // Wrap the methods inside delegate instances and pass to the method.
            WriteOutput("perls"new UppercaseDelegate(UppercaseFirst));
            WriteOutput("perls"new UppercaseDelegate(UppercaseLast));
            WriteOutput("perls"new UppercaseDelegate(UppercaseAll));
 
            UppercaseDelegate d=delegate(string s){
                return s.ToUpper();
            };
            WriteOutput(" UppercaseDelegate d=delegate(string s)",d);
 
            //Anonymous  methods
            WriteOutput("DDDD"delegate(string s)
            {
                return s.ToLower();
            });
            //lambda expression
            WriteOutput("FFFF",new UppercaseDelegate(x=>x.ToLower()));
 
            /*
             Using lambda expressions
             */
            //
            // Use implicitly typed lambda expression.
            // ... Assign it to a Func instance.
            //
            Func<intint> func1 = x => x + 1;
            //
            // Use lambda expression with statement body.
            //
            Func<intint> func2 = x =>
            {
                return x + 1;
            };
            //
            // Use formal parameters with expression body.
            //
            Func<intint> func3 = (int x) => x + 1;
 
            //
            // Use parameters with a statement body.
            //
            Func<intint> func4 = (int x) =>
            {
                return x + 1;
            };
            //
            // Use multiple parameters.
            //
            Func<intintint> func5 = (x, y) => x * y;
 
            //
            // Use no parameters in a lambda expression.
            //
            Action func6 = () => Console.WriteLine(" Use no parameters in a lambda expression");
 
            //
            // Use delegate method expression.
            //
            Func<intint> func7 = delegate(int x)
            {
                return x + 1;
            };
 
            //
            // Use delegate expression with no parameter list.
            //
            Func<int> func8 = delegate
            {
                return 1 + 1;
            };
 
            //
            // Invoke each of the lambda expressions and delegates we created.
            // ... The methods above are executed.
            //
            Console.WriteLine(func1.Invoke(1));
            Console.WriteLine(func2.Invoke(1));
            Console.WriteLine(func3.Invoke(1));
            Console.WriteLine(func4.Invoke(1));
            Console.WriteLine(func5.Invoke(2, 2));
            func6.Invoke();
            Console.WriteLine(func7.Invoke(1));
            Console.WriteLine(func8.Invoke());
 
            Console.ReadLine();
 
        }
    }
}
發佈了110 篇原創文章 · 獲贊 5 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章