anonymous-methods

Why Local Functions generate IL different from Anonymous Methods and Lambda Expressions?

夙愿已清 提交于 2019-12-04 00:45:25
Why the C# 7 Compiler turns Local Functions into methods within the same class where their parent function is. While for Anonymous Methods (and Lambda Expressions) the compiler generates a nested class for each parent function, that will contain all of its Anonymous Methods as instance methods ? For example, C# code (Anonymous Method) : internal class AnonymousMethod_Example { public void MyFunc(string[] args) { var x = 5; Action act = delegate () { Console.WriteLine(x); }; act(); } } Will produce IL Code (Anonymous Method) similar to: .class private auto ansi beforefieldinit AnonymousMethod

How to yield return inside anonymous methods?

微笑、不失礼 提交于 2019-12-03 23:19:18
问题 Basically I have an anonymous method that I use for my BackgroundWorker : worker.DoWork += ( sender, e ) => { foreach ( var effect in GlobalGraph.Effects ) { // Returns EffectResult yield return image.Apply (effect); } }; When I do this the compiler tells me: "The yield statement cannot be used inside an anonymous method or lambda expression" So in this case, what's the most elegant way to do this? Btw this DoWork method is inside a static method, in case that matters for the solution. 回答1:

Convert this delegate to an anonymous method or lambda

泪湿孤枕 提交于 2019-12-03 18:57:40
问题 I am new to all the anonymous features and need some help. I have gotten the following to work: public void FakeSaveWithMessage(Transaction t) { t.Message = "I drink goats blood"; } public delegate void FakeSave(Transaction t); public void SampleTestFunction() { Expect.Call(delegate { _dao.Save(t); }).Do(new FakeSave(FakeSaveWithMessage)); } But this is totally ugly and I would like to have the inside of the Do to be an anonymous method or even a lambda if it is possible. I tried: Expect.Call

Serializing a list of anonymous delegates

你说的曾经没有我的故事 提交于 2019-12-03 17:37:53
This question may be very similar to my one, but I cannot see the answer I need in it. I have a class, called CASM , that has a List<Action> . I want to serialize this class (using the BinaryFormatter or something similar). This class and all classes referenced in the Action s have got correct [Serializable] and [NonSerializable] attributes. The problem comes when serialization is attempted - it gives this error: Type 'CASM.CASM+<>c__DisplayClass2c' in Assembly 'CASM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. This <>c__DisplayClass2c is an

Why is an out parameter not allowed within an anonymous method?

£可爱£侵袭症+ 提交于 2019-12-03 11:40:00
This is not a dupe of Calling a method with ref or out parameters from an anonymous method I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters, not as much. what are your thoughts on this JaredPar In some ways this is a dupe. Out parameters are ref parameters. There is simply an extra attribute on the value that is used by the C# language. The reason for disallowing them is the exact same as ref parameters. The problem here originates with the effect of using a value declared outside the

Delegates and Lambdas and LINQ, Oh My!

核能气质少年 提交于 2019-12-03 09:15:44
问题 As a fairly junior developer, I'm running into a problem that highlights my lack of experience and the holes in my knowledge. Please excuse me if the preamble here is too long. I find myself on a project that involves my needing to learn a number of new (to me) technologies, including LINQ (to OBJECTS and to XML for purposes of this project) among others. Everything I've read to this point suggests that to utilize LINQ I'll need to fully understand the following (Delegates, Anonymous Methods

Create anonymous method from a string in c#

≯℡__Kan透↙ 提交于 2019-12-03 08:58:41
is it possible to create an anonymous method in c# from a string? e.g. if I have a string "x + y * z" is it possible to turn this into some sort of method/lambda object that I can call with arbitrary x , y , z parameters? It's possible, yes. You have to parse the string and, for example, compile a delegate using expression trees. Here's an example of creating (x, y, z) => x + y * z using expression trees: ParameterExpression parameterX = Expression.Parameter(typeof(int), "x"); ParameterExpression parameterY = Expression.Parameter(typeof(int), "y"); ParameterExpression parameterZ = Expression

Dynamically assigning function implementation in Python

筅森魡賤 提交于 2019-12-03 07:24:13
问题 I want to assign a function implementation dynamically. Let's start with the following: class Doer(object): def __init__(self): self.name = "Bob" def doSomething(self): print "%s got it done" % self.name def doItBetter(self): print "Done better" In other languages we would make doItBetter an anonymous function and assign it to the object. But no support for anonymous functions in Python. Instead, we'll try making a callable class instance, and assign that to the class: class Doer(object): def

Why don't anonymous delegates/lambdas infer types on out/ref parameters?

筅森魡賤 提交于 2019-12-03 06:07:58
Several C# questions on StackOverflow ask how to make anonymous delegates/lambdas with out or ref parameters. See, for example: Calling a method with ref or out parameters from an anonymous method Write a lambda or anonymous function that accepts an out parameter To do so, you just need to specify the type of the parameter, as in: public void delegate D(out T p); // ... D a = (out T t) => { ... }; // Lambda syntax. D b = delegate(out T t) { ... }; // Anonymous delegate syntax. What I'm curious about is why the type is explicitly required. Is there a particular reason that this is the case?

Is there a case where delegate syntax is preferred over lambda expression for anonymous methods?

孤街浪徒 提交于 2019-12-03 05:56:30
With the advent of new features like lambda expressions (inline code), does it mean we dont have to use delegates or anonymous methods anymore? In almost all the samples I have seen, it is for rewriting using the new syntax. Any place where we still have to use delegates and lambda expressions won't work? Yes there are places where directly using anonymous delegates and lambda expressions won't work. If a method takes an untyped Delegate then the compiler doesn't know what to resolve the anonymous delegate/lambda expression to and you will get a compiler error. public static void Invoke