- 浏览: 401291 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1979first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1930Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6555Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12054标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1253DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2576I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10955In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1296If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 943When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 819I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1926You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5337As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1191Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1717P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6100NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1227It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1100WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 870In WCF, primitives type are s ...
相关推荐
/ mi> </ msub> <mo>→</ mo> <msup> <mi> N </ mi> <mo> ∗ </ mo> </ msup> <msup> <mi>ℓ</ mi> < mo> + </ mo> </ msup> <msup> <mi>ℓ</ mi> <mo>-</ mo> </ msup> </ mrow> </ math>衰减是在 轻锥体QCD...
`functional`文件夹很可能包含实现了一些C++标准库`<functional>`中功能的对象,如函数对象、函数适配器等,这些在使用lambda时可能会用到。而`initializer_list`文件可能包含了对`std::initializer_list`类型的实现...
本测试着重关注四种常见的集合类型:ArrayList、Dictionary<TKey, TValue>、List<T>以及DataSet,它们各自在遍历性能上的差异。遍历性能是衡量代码执行效率的重要指标,尤其在大数据量处理时,高效的遍历方法能显著...
在C#编程中,`List<T>` 是一个非常常用的集合类,它提供了丰富的操作方法,如查找、添加、删除等。在本篇文章中,我们将详细探讨`List<T>`的`FindAll`方法,并通过四种不同的写法来演示如何使用这个功能来筛选满足...
在C#编程中,`List<T>` 是一个常用的集合类,用于存储同类型的元素序列。在处理这些数据时,我们经常需要对它们进行排序。`OrderBy` 方法是 LINQ(Language Integrated Query) 提供的一个非常方便的排序工具,它...
在本文档中,我们将深入探讨Lambda表达式及其在软件开发中的应用,特别是与泛型类型如List<T>相关的操作。 首先,Lambda表达式在处理集合时提供了极大的便利。例如,当你需要对一系列同类型的集合执行操作,如添加...
The Scheme<br>Programming Language<br><br>... Multitasking with Engines <br><br>Bibliography<br><br>Answers to Selected Exercises<br><br>Formal Syntax of Scheme<br><br>Summary of Forms<br><br>Index<br>
1590597893 and Pub Date: November 19, 2007<br> The size of the release is: 03 disks x 2.88mb<br> And released on: 12/22/2007<br><br>===================================================<br> LINQ is the...
在C#编程中,泛型列表`List<T>`是一个常用的数据结构,它允许存储类型统一的元素集合。本文将深入探讨`List<T>`的基本用法,帮助开发者更好地理解和使用这一功能。 1. **创建和初始化** - `List<T>`可以通过无参...
<br><br> <br><br>System monitoring 系统监控 <br><br> <br><br>Memory monitoring 内存监控 <br><br> <br><br>Cluster information 集群信息 <br><br> <br><br>Log file tailing 实时追踪log文件 <br><br> <br><br...
本主题聚焦于如何将`Expression<Func<T>>`转换为`Expression<Func<T, object>>`,这在某些场景下是必要的,比如当我们想要返回一个匿名类型或进行更灵活的查询操作时。 `Expression<Func<T>>`表示的是一个无参数的...
接下来我们就通过几个基本常用的及非典型的案例来说明Lambda表达式的使用 List转换为List<T.ID> ...List<Map<String,Object>>转Map<String,T> List<Map<String,Object>>转Map<String,Map<String,Object>
<br>Publisher: Addison Wesley Professional <br>Pub Date: August 31, 2005 <br>ISBN: 0321133544 <br>Pages: 432 <br> <br> <br><br> <br><br>Table of Contents | Index <br> <br>Introducing the Boost ...
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda() .like(User::getName, name) .eq(User::getEmail, email); List<User> users = userService.selectUsersByCondition(query...
practice solutions for performing type conversions and lexical conversions<br><br>Utility ...and Boost.Lambda<br><br>More flexible callbacks with Boost.Function<br><br>Managed signals and slots (a.k.a....
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>...
List<People> people = new List<People>(); People p1 = new People(21,"guojing"); People p2 = new People(21, "wujingmin"); People p3 = new People(20, "muqing"); People p4 = new People(23, "lupan"); ...
在C#编程中,我们经常需要操作集合,如List<T>,进行数据的增删改查。本主题聚焦于如何修改List中所有对象的某个特定字段。Lambda表达式和ForEach方法是实现这一目标的常用工具。下面将详细介绍这两种方法。 **...
根据给定文件中的标题、描述、标签以及部分内容,我们可以总结出以下有关C#中的SQL、LINQ和Lambda表达式之间的转换知识点。 ### C#中的SQL、LINQ与Lambda表达式的对比 #### 1. 基础查询 **SQL:** ```sql SELECT * ...