anonymous-methods

Discrete Anonymous methods sharing a class?

怎甘沉沦 提交于 2019-11-27 09:06:24
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 be collected until vart1 is collected, which may happen much later than after joik returns? After all

Cannot use 'this' in member initializer?

戏子无情 提交于 2019-11-27 07:44:07
问题 Is this legal? Does it contain a hidden bug or flaw? Visual studio does not give any errors or warnings but ReSharper does: /// <summary> /// immutable tuple for two /// </summary> public class Pair<TValue1, TValue2> : Singleton<TValue1> { public TValue2 Value2 { get; private set; } public Pair(TValue1 value1, TValue2 value2, Func<Pair<TValue1, TValue2>, String> toStringFunc) : this(value1, value2, () => toStringFunc(this)) { } //Red light }2> : Singleton<TValue1> 回答1: I'm pretty sure I've

Why can't c# use inline anonymous lambdas or delegates? [duplicate]

隐身守侯 提交于 2019-11-27 06:59:09
This question already has an answer here: Why must a lambda expression be cast when supplied as a plain Delegate parameter 7 answers 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 delegate body, and avoid capturing it in a named variable (making it anonymous)? // Inline anonymous lambda not allowed Console

How to circumvent using an out parameter in an anonymous method block?

走远了吗. 提交于 2019-11-27 06:55:16
问题 The following method does not compile. Visual Studio warns "An out parameter may not be used within an anonymous method". The WithReaderLock(Proc action) method takes a delegate void Proc() . public Boolean TryGetValue(TKey key, out TValue value) { Boolean got = false; WithReaderLock(delegate { got = dictionary.TryGetValue(key, out value); }); return got; } What's the best way to get this behavior? (Please refrain from providing advice on threadsafe dictionaries, this question is intended to

How are anonymous methods implemented under the hood?

谁都会走 提交于 2019-11-27 06:46:12
问题 Does Delphi "instantiate" each anonymous method (like an object)?, if so when does Delphi create this instance, and most important, when does Delphi free it? Because anonymous method also captures external variables and extends their life time, it's important to know when these variables will be "released" from the memory. What are the possible drawbacks to declare an anonymous method inside another anonymous method. Are circular reference possible? 回答1: Anonymous methods are implemented as

How to identify anonymous methods in System.Reflection

扶醉桌前 提交于 2019-11-27 06:07:14
问题 How can you identify anonymous methods via reflection? 回答1: Look at the attributes of the method, and see if the method is decorated with CompilerGeneratedAttribute. Anonymous methods (as well as other objects, such as auto-implemented properties, etc) will have this attribute added. For example, suppose you have a type for your class. The anonymous methods will be in: Type myClassType = typeof(MyClass); IEnumerable<MethodInfo> anonymousMethods = myClassType .GetMethods( BindingFlags

C# -Closure -Clarification

纵饮孤独 提交于 2019-11-27 02:49:07
问题 I am learning C#.Can I mean closure as a construct that can adopt the changes in the environment in which it is defined. Example : List<Person> gurus = new List<Person>() { new Person{id=1,Name="Jon Skeet"}, new Person{id=2,Name="Marc Gravell"}, new Person{id=3,Name="Lasse"} }; void FindPersonByID(int id) { gurus.FindAll(delegate(Person x) { return x.id == id; }); } The variable id is declared in the scope of FindPersonByID() but t we still can access the local variable id inside the

C# Cannot use ref or out parameter inside an anonymous method body

本小妞迷上赌 提交于 2019-11-27 02:07:37
问题 I'm trying to create a function that can create an Action that increments whatever integer is passed in. However my first attempt is giving me an error "cannot use ref or out parameter inside an anonymous method body". public static class IntEx { public static Action CreateIncrementer(ref int reference) { return () => { reference += 1; }; } } I understand why the compiler doesn't like this, but nonetheless I'd like to have a graceful way to provide a nice incrementer factory that can point to

Can an anonymous method in C# call itself?

梦想的初衷 提交于 2019-11-27 00:41:49
问题 I have the following code: class myClass { private delegate string myDelegate(Object bj); protected void method() { myDelegate build = delegate(Object bj) { var letters= string.Empty; if (someCondition) return build(some_obj); //This line seems to choke the compiler else string.Empty; }; ...... } } Is there another way to set up an anonymous method in C# such that it can call itself? 回答1: You can break it down into two statements and use the magic of captured variables to achieve the

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

亡梦爱人 提交于 2019-11-26 23:14:15
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 reason for why it is this way, and perhaps someone knows? Furthermore, does anyone know if it will be