C#中的lambda表達式

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

namespace ConsoleApplication1
{
	//事件訂閱器
	class Program
	{
		static void Main(string[] args)
		{
			string[] words = { "bot", "apple", "apricot" };
			int minimalLength = words.Where(w => w.StartsWith("a"))
				.Max(x => x.Length);
			Console.WriteLine(minimalLength);

			int[] numbers = { 1, 4, 7, 10 };
			int product = numbers.Aggregate(2, (interim, next) => interim * next);
			Console.WriteLine(product);


			int product1 = numbers.Aggregate(2, (int interim, int next) => interim * next);
			Console.WriteLine(product1);

			//表示這個方法是沒有參數的,只有一個返回值爲string類型
			Func<string> greet = () => "Hello,World";
			Console.WriteLine(greet());

			//表示這個方法具有一個參數和一個返回值
			Func<int, int> square = x => x * x;
			Console.WriteLine(square(5));

			System.Linq.Expressions.Expression<Func<int, int>> e = x => x * x;
			Console.WriteLine(e);

			int[] numbers1 = { 2, 3, 4, 5 };
			var squaredNumbers = numbers1.Select(x => x * x);
			Console.WriteLine(string.Join(" ", squaredNumbers));

			//封裝一個方法不帶有參數的(沒有返回值)
			Action line = () => Console.WriteLine("該方法是未帶有參數的");
			//封裝一個方法帶有一個參數(沒有返回值)
			Action<int> line2 = (num) => Console.WriteLine(num);
			//封裝一個方法帶有兩個參數(沒有返回值)
			Action<int,int> line3 = (num1,num2) => Console.WriteLine(num1+num2);

			//封裝一個方法帶有一個參數並且帶有一個返回值
			Func<int, int> func1 = a => a + a;
			Console.WriteLine(func1(5));
			//封裝一個方法並且帶有兩個參數和一個返回值
			Func<int, int, bool> testForEquality = (x, y) => x == y;
			Console.WriteLine(testForEquality(6,6));

			Action<string> greet1 = name =>
			{
				string greeting = $"Hello{name}!";
				Console.WriteLine(greeting);
			};
			greet1("World");

			int[] numbers2 = {2,2 , 1, 3, 9,2, 8, 6, 7, 2, 0 };
			int oddNumbers2 = numbers2.Count(n => n % 2 == 1);
			Console.WriteLine($"There are {oddNumbers2} odd numbers in{string.Join(" ", numbers2)}");

			var firstNumbersLessThanSix = numbers.TakeWhile(n => n < 6);
			Console.WriteLine(string.Join(" ", firstNumbersLessThanSix));

			var firsetSmallNumbers = numbers2.TakeWhile((n, index) => n > index);
			Console.WriteLine(string.Join(" ", firsetSmallNumbers));

			//一下程序塊是對VariableCaptureGame類中的方法調用
			var game = new VariableCaptureGame();

			int gameInput = 5;
			game.Run(gameInput);     

			int jTry = 10;
			bool result = game.isEqualToCapturedLcaoVariable(jTry);
			Console.WriteLine($"Captured local variable is equal to{jTry}");

			int anotherJ = 3;
			game.updateCaputeredLocalVariable(anotherJ);

			bool euqualToAnother = game.isEqualToCapturedLcaoVariable(anotherJ);
			Console.WriteLine($"Another lambda obser a new value of capture variable:{euqualToAnother}");
			Console.ReadKey();
		}

 
			public class VariableCaptureGame
			{
				//internal就是把訪問權限限制在程序集之內
				internal Action<int> updateCaputeredLocalVariable;
				internal Func<int, bool> isEqualToCapturedLcaoVariable;

				public void Run(int input)
				{
					int j = 0;
					updateCaputeredLocalVariable = (x) =>
					{
						j = x;
						bool result = j > input;
						Console.WriteLine($"{j} is greater than{input}:{result}");
					};

					isEqualToCapturedLcaoVariable = x => x == j;

					Console.WriteLine($"Local variable before lambda invocation:{j}");
					//調用委託
					updateCaputeredLocalVariable(10);
					Console.WriteLine($"Local vaiable after lambda invocation:{j}");
				}
			}
	 
	}



}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章