c# - Sample of Lambda expression on List<T> and Events

This is not rocket science, but it does do server a good example on how to use the expression on List<T> and event so that you can refer back some point in life.

 

First, we see List<T> classes has such methods: FindAll which takes a Predicate<T>, Sort takes a Comparison<T> and ForEach takes a Action<T>, below shows the code that uses Lambda expression to initialize the Predicate<T>, Comparision<T> and Action<T>...

 

Here is the code 

  public class SampleWithEventsAndLambdaOnList
  {

    class Film
    {
      public string Name { get; set; }
      public int Year { get; set; } 
    }

    public static void DemoSampleWithLambdaOnList()
    {
      var films = new List<Film>
      {
        new Film { Name = "Jaws", Year = 1975 },
        new Film { Name = "Sining in the Rain", Year = 1952},
        new Film { Name = "Some Like it Hot", Year = 1959},
        new Film { Name = "The wizard of Oz", Year = 1939 },
        new Film { Name = "It's a Wonderful Life", Year = 1946 },
        new Film { Name = "American Beauty", Year = 1999 },
      };
      Action<Film> print = film => Console.WriteLine("Name={0}, Year={1}", film.Name, film.Year);

      films.ForEach(print);
      films.FindAll(film => film.Year > 1960).ForEach(print);

      films.Sort((f1, f2) => f1.Name.CompareTo(f2.Name));
      films.ForEach(print);
    }
}
 

Second, let's see the example of adding some log to Control's events. First see the code below. 

  public class SampleWithEventsAndLambdaOnList
  {
    public static void Log(string title, object sender, EventArgs e)
    {
      Console.WriteLine("Event: {0}", title);
      Console.WriteLine("  Sender: {0}", sender);
      Console.WriteLine("  Arguments: {0}", e.GetType());

      foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(e))
      {
        string name = prop.DisplayName;
        object value = prop.GetValue(e);
        Console.WriteLine("   {0} ={1}", name, value);
      }
    }

    public static void DemoSamleEventOnList()
    {
      Button button = null;
      //var form = new Form();
      //form.Controls.AddRange(new Control[] { 
      //  new Label { Text = "Simple Demo Form" },
      //  button = new Button { Text = "Click Me"}
      //});
      button = new Button { Text = "Click Me" } ;
      button.Click += (src, e) => Log("Click ", src, e);
      button.KeyPress += (src, e) => Log("KeyPress", src, e);
      button.MouseClick += (src, e) => Log("MouseClick", src, e);

      var form = new Form { AutoSize = true, Controls = { button } }; // the reason why this works is because Controls = { button } will actually transform to 
                                                                      // Controls.AddRange(new [] { button } )
                                                                      // but if you write as such 
                                                                      //  Controls = new [] { button }
                                                                      // then it is error
      Application.Run(form);

    }
  }
 

As you can see, you can easily replace the delegate declaration with the lambda expression

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值