C#动态匿名函数用法示例

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

namespace ConsoleApplication2
{
	public class Temp
	{
		public string name { get; set; }
		public int value { get; set; }
	}

	class Program
	{
		static void Main(string[] args)
		{
			List<Temp>  list = new List<Temp>();  //假定这是来自其他某函数的返回值,一个很长的list

			var resultList = new List<Temp>() { };

			bool a = RandomTF();

			//Func<bool, Temp> test = null;

			Predicate<Temp> match;
			if (a)
			{
				match = temp => temp.value > 0;
			}
			else
			{
				match = temp => temp.value > 0 && temp.name.Length > 3;
			}

			resultList = list.FindAll(match);

			var b = resultList;
		}


		private static bool RandomTF()
		{
			Random r = new Random();
			var a = r.Next(1,10);
			return a % 2 == 0;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/festone000/article/details/78321383