anonymous-methods

Using MethodInfo.GetCurrentMethod() in anonymous methods

笑着哭i 提交于 2019-12-01 16:49:17
public static void Main(string[] args) { Action a = () => Console.WriteLine(MethodInfo.GetCurrentMethod().Name); a(); } This code will return an obscure string like so: <Main>b__0 . Is there a way of ignoring the anonymous methods and get a more readable method name? You could capture it outside: var name = MethodInfo.GetCurrentMethod().Name + ":subname"; Action a = () => Console.WriteLine(name); Other than that; no. No, there isn't. That's why it is an anonymous method. The name is automatically generated by the compiler and guaranteed to be unique. If you want to get the calling method name

Using MethodInfo.GetCurrentMethod() in anonymous methods

半腔热情 提交于 2019-12-01 15:48:16
问题 public static void Main(string[] args) { Action a = () => Console.WriteLine(MethodInfo.GetCurrentMethod().Name); a(); } This code will return an obscure string like so: <Main>b__0 . Is there a way of ignoring the anonymous methods and get a more readable method name? 回答1: You could capture it outside: var name = MethodInfo.GetCurrentMethod().Name + ":subname"; Action a = () => Console.WriteLine(name); Other than that; no. 回答2: No, there isn't. That's why it is an anonymous method. The name is

Why does CLR create new class for anonymous method?

≯℡__Kan透↙ 提交于 2019-12-01 11:46:45
I am using anonymous functions in my projects no less. And till know I was thinking that, C# compiler generates just a method using the code used for the anonymous method in the same class . But, after decompiling this code in IL, I saw that CLR created a new class. public class Comparer { public delegate int Greater(int a, int b); public int Great(Greater greater, int a, int b) { return greater(a, b); } } static void Main(string[] args) { int valueOfA = 11, valueOfB = 23, valueOfC = 42; Comparer comparer = new Comparer(); Console.WriteLine("The greater is \t:{0}", comparer.Great(delegate(int

How to return a generic list collection in C#?

我只是一个虾纸丫 提交于 2019-12-01 05:50:37
I have some linq to sql method and when it does the query it returns some anonymous type. I want to return that anonymous type back to my service layer to do some logic and stuff on it. I don't know how to return it though. I thought I could do this public List<T> thisIsAtest() { return query; } but I get this error Error 1 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) So not sure what assembly I am missing or if that is even the case. Thanks EDIT Ok my first problem was solved but now I have a new problem that I am not sure how

local variable scope in linq anonymous method ( closure)

大憨熊 提交于 2019-12-01 04:46:50
What is the scope of local variable declared in Linq Query. I was writing following code static void Evaluate() { var listNumbers = Enumerable.Range(1, 10).Select(i => i); int i = 10; } Compiler flagged error on line int i=10, stating A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else I am unable to understand why this error is coming. My understanding was that i will become out of scope after first line (in foreach loop). So i can be declared again. Actual behavior is

How do lambda expressions work internally?

こ雲淡風輕ζ 提交于 2019-12-01 04:08:16
While looking up the answer to this question: " Why is an out parameter not allowed within an anonymous method? " I've got a little lost about how do lambda expression and anonymous methods actually work. In the comments JaredPar states that "Imagine for instance that the out parameter referred to a local variable on the stack. The lambda can execute at any arbitrary point in the future and hence could execute when that stack frame was no longer valid". I pointed out if wouldn't that be the case with any other variable... which basically make me wonder what to I really know about lambda

Assign an anonymous method to an interface variable or parameter?

梦想的初衷 提交于 2019-12-01 02:49:31
Anonymous methods are essentially interface s with an Invoke method: type TProc = reference to procedure; IProc = interface procedure Invoke; end; Now, is there a possibility to assign them to an actual interface variable or pass them as interface parameter? procedure TakeInterface(const Value: IInterface); begin end; var P: TProc; I: IInterface; begin I := P; // E2010 TakeInterface(P); // E2010 end; [DCC32 Error] E2010 Incompatible types: 'IInterface' and 'procedure, untyped pointer or untyped parameter' Question: What would be the use case for this? There are a lot of objects out there, that

local variable scope in linq anonymous method ( closure)

被刻印的时光 ゝ 提交于 2019-12-01 02:25:26
问题 What is the scope of local variable declared in Linq Query. I was writing following code static void Evaluate() { var listNumbers = Enumerable.Range(1, 10).Select(i => i); int i = 10; } Compiler flagged error on line int i=10, stating A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else I am unable to understand why this error is coming. My understanding was that i will

How to yield return inside anonymous methods?

时间秒杀一切 提交于 2019-12-01 02:05:31
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. Unfortunately you can't. The compiler does not allow you to combine the two "magic" pieces of code. Both

Assign an anonymous method to an interface variable or parameter?

别等时光非礼了梦想. 提交于 2019-11-30 22:41:16
问题 Anonymous methods are essentially interface s with an Invoke method: type TProc = reference to procedure; IProc = interface procedure Invoke; end; Now, is there a possibility to assign them to an actual interface variable or pass them as interface parameter? procedure TakeInterface(const Value: IInterface); begin end; var P: TProc; I: IInterface; begin I := P; // E2010 TakeInterface(P); // E2010 end; [DCC32 Error] E2010 Incompatible types: 'IInterface' and 'procedure, untyped pointer or