anonymous-methods

Calling newly defined method from anonymous class

房东的猫 提交于 2019-11-26 23:13:30
问题 I instantiated an object of an anonymous class to which I added a new method. Date date = new Date() { public void someMethod() {} } I am wondering if it is possible to call this method from outside somehow similar to: date.someMethod(); 回答1: Good question. Answer is No. You cannot directly call date.someMethod(); Let's understand first what is this. Date date = new Date() { ... }; Above is anonymous(have no name) sub-class which is extending Date class. When you see the code like, Runnable r

When not to use lambda expressions [closed]

这一生的挚爱 提交于 2019-11-26 22:32:23
问题 A lot of questions are being answered on Stack Overflow, with members specifying how to solve these real world/time problems using lambda expressions. Are we overusing it, and are we considering the performance impact of using lambda expressions? I found a few articles that explores the performance impact of lambda vs anonymous delegates vs for / foreach loops with different results Anonymous Delegates vs Lambda Expressions vs Function Calls Performance Performance of foreach vs. List.ForEach

Declaring Func<in T, out Result> dynamically

天大地大妈咪最大 提交于 2019-11-26 21:23:32
问题 Consider this: var propertyinfo = typeof(Customer).GetProperty(sortExpressionStr); Type orderType = propertyinfo.PropertyType; now I want to declare Func<int,orderType> I know its not possible directly since ordertype is at runtime but is there is any workaround ? this is exactly what I want to do : var propertyinfo = typeof(T).GetProperty(sortExpressionStr); Type orderType = propertyinfo.PropertyType; var param = Expression.Parameter(typeof(T), "x"); var sortExpression = (Expression.Lambda

How do I Unregister 'anonymous' event handler [duplicate]

耗尽温柔 提交于 2019-11-26 18:52:50
This question already has an answer here: Unsubscribe anonymous method in C# 11 answers Say if I listen for an event: Subject.NewEvent += delegate(object sender, NewEventArgs e) { //some code }); Now how do I un-register this event? Or just allow the memory to leak? If you need to unregister an event, I recommend avoiding anonymous delegates for the event handler. This is one case where assigning this to a local method is better - you can unsubscribe from the event cleanly. Give your instance of the anonymous delegate a name: EventHandler<NewEventArg> handler = delegate(object sender,

In C#, why can't an anonymous method contain a yield statement?

五迷三道 提交于 2019-11-26 17:57:21
问题 I thought it would be nice to do something like this (with the lambda doing a yield return): public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new() { IList<T> list = GetList<T>(); var fun = expression.Compile(); var items = () => { foreach (var item in list) if (fun.Invoke(item)) yield return item; // This is not allowed by C# } return items.ToList(); } However, I found out that I can't use yield in anonymous method. I'm wondering why. The yield docs just say it

Discrete Anonymous methods sharing a class?

半城伤御伤魂 提交于 2019-11-26 14:29:36
问题 I was playing a bit with Eric Lippert's Ref<T> class from here. I noticed in the IL that it looked like both anonymous methods were using the same generated class, even though that meant the class had an extra variable. While using only one new class definition seems somewhat reasonable, it strikes me as very odd that only one instance of <>c__DisplayClass2 is created. This seems to imply that both instances of Ref<T> are referencing the same <>c__DisplayClass2 Doesn't that mean that y cannot

Why can&#39;t c# use inline anonymous lambdas or delegates? [duplicate]

▼魔方 西西 提交于 2019-11-26 12:59:21
问题 This question already has answers here : Why must a lambda expression be cast when supplied as a plain Delegate parameter (8 answers) Closed 6 years ago . I hope I worded the title of my question appropriately. In c# I can use lambdas (as delegates), or the older delegate syntax to do this: Func<string> fnHello = () => \"hello\"; Console.WriteLine(fnHello()); Func<string> fnHello2 = delegate() { return \"hello 2\"; }; Console.WriteLine(fnHello2()); So why can\'t I \"inline\" the lambda or the

Adding and Removing Anonymous Event Handler

匆匆过客 提交于 2019-11-26 09:27:47
问题 I was wondering if this actually worked ? private void RegisterKeyChanged(T item) { item.OnKeyChanged += (o, k) => ChangeItemKey((T)o, k); } private void UnRegisterKeyChanged(T item) { item.OnKeyChanged -= (o, k) => ChangeItemKey((T)o, k); } How does the compiler know that the event handlers are the same ? Is this even recommended? 回答1: There's an MSDN page that talks about this: How to Subscribe to and Unsubscribe from Events Note in particular: If you will not have to unsubscribe to [sic]

Why can I not edit a method that contains an anonymous method in the debugger?

冷暖自知 提交于 2019-11-26 08:37:17
问题 So, every time I have written a lambda expression or anonymous method inside a method that I did not get quite right, I am forced to recompile and restart the entire application or unit test framework in order to fix it. This is seriously annoying, and I end up wasting more time than I saved by using these constructs in the first place. It is so bad that I try to stay away from them if I can, even though Linq and lambdas are among my favourite C# features. I suppose there is a good technical

How do I Unregister &#39;anonymous&#39; event handler [duplicate]

孤街浪徒 提交于 2019-11-26 06:39:27
问题 This question already has answers here : Unsubscribe anonymous method in C# (11 answers) Closed 5 years ago . Say if I listen for an event: Subject.NewEvent += delegate(object sender, NewEventArgs e) { //some code }); Now how do I un-register this event? Or just allow the memory to leak? 回答1: If you need to unregister an event, I recommend avoiding anonymous delegates for the event handler. This is one case where assigning this to a local method is better - you can unsubscribe from the event