|
|
|
@ -22,19 +22,19 @@ using System.Linq;
@@ -22,19 +22,19 @@ using System.Linq;
|
|
|
|
|
|
|
|
|
|
public static class DelegateConstruction |
|
|
|
|
{ |
|
|
|
|
class InstanceTests |
|
|
|
|
private class InstanceTests |
|
|
|
|
{ |
|
|
|
|
public Action CaptureOfThis() |
|
|
|
|
{ |
|
|
|
|
return delegate { |
|
|
|
|
CaptureOfThis(); |
|
|
|
|
return (Action)delegate { |
|
|
|
|
this.CaptureOfThis(); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Action CaptureOfThisAndParameter(int a) |
|
|
|
|
{ |
|
|
|
|
return delegate { |
|
|
|
|
CaptureOfThisAndParameter(a); |
|
|
|
|
return (Action)delegate { |
|
|
|
|
this.CaptureOfThisAndParameter(a); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -42,8 +42,8 @@ public static class DelegateConstruction
@@ -42,8 +42,8 @@ public static class DelegateConstruction
|
|
|
|
|
{ |
|
|
|
|
foreach (int item in Enumerable.Empty<int>()) { |
|
|
|
|
if (item > 0) { |
|
|
|
|
return delegate { |
|
|
|
|
CaptureOfThisAndParameter(item + a); |
|
|
|
|
return (Action)delegate { |
|
|
|
|
this.CaptureOfThisAndParameter(item + a); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -55,8 +55,8 @@ public static class DelegateConstruction
@@ -55,8 +55,8 @@ public static class DelegateConstruction
|
|
|
|
|
foreach (int item in Enumerable.Empty<int>()) { |
|
|
|
|
int copyOfItem = item; |
|
|
|
|
if (item > 0) { |
|
|
|
|
return delegate { |
|
|
|
|
CaptureOfThisAndParameter(item + a + copyOfItem); |
|
|
|
|
return (Action)delegate { |
|
|
|
|
this.CaptureOfThisAndParameter(item + a + copyOfItem); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -66,7 +66,7 @@ public static class DelegateConstruction
@@ -66,7 +66,7 @@ public static class DelegateConstruction
|
|
|
|
|
public void LambdaInForLoop() |
|
|
|
|
{ |
|
|
|
|
for (int i = 0; i < 100000; i++) { |
|
|
|
|
Bar(() => Foo()); |
|
|
|
|
this.Bar((Func<int>)(() => this.Foo())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -117,14 +117,11 @@ public static class DelegateConstruction
@@ -117,14 +117,11 @@ public static class DelegateConstruction
|
|
|
|
|
public static List<Action<int>> AnonymousMethodStoreWithinLoop() |
|
|
|
|
{ |
|
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
|
|
for (int i = 0; i < 10; i++) |
|
|
|
|
{ |
|
|
|
|
for (int i = 0; i < 10; i++) { |
|
|
|
|
int counter; |
|
|
|
|
list.Add(delegate(int x) |
|
|
|
|
{ |
|
|
|
|
list.Add((Action<int>)delegate(int x) { |
|
|
|
|
counter = x; |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
return list; |
|
|
|
|
} |
|
|
|
@ -133,21 +130,17 @@ public static class DelegateConstruction
@@ -133,21 +130,17 @@ public static class DelegateConstruction
|
|
|
|
|
{ |
|
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
|
|
int counter; |
|
|
|
|
for (int i = 0; i < 10; i++) |
|
|
|
|
{ |
|
|
|
|
list.Add(delegate(int x) |
|
|
|
|
{ |
|
|
|
|
for (int i = 0; i < 10; i++) { |
|
|
|
|
list.Add((Action<int>)delegate(int x) { |
|
|
|
|
counter = x; |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
return list; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Action StaticAnonymousMethodNoClosure() |
|
|
|
|
{ |
|
|
|
|
return delegate |
|
|
|
|
{ |
|
|
|
|
return (Action)delegate { |
|
|
|
|
Console.WriteLine(); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
@ -156,16 +149,15 @@ public static class DelegateConstruction
@@ -156,16 +149,15 @@ public static class DelegateConstruction
|
|
|
|
|
{ |
|
|
|
|
// i is captured variable,
|
|
|
|
|
// j is parameter in anonymous method
|
|
|
|
|
// k is local in anonymous method,
|
|
|
|
|
// l is local in main method
|
|
|
|
|
// l is local in anonymous method,
|
|
|
|
|
// k is local in main method
|
|
|
|
|
// Ensure that the decompiler doesn't introduce name conflicts
|
|
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
|
|
for (int l = 0; l < 10; l++) { |
|
|
|
|
for (int k = 0; k < 10; k++) { |
|
|
|
|
int i; |
|
|
|
|
for (i = 0; i < 10; i++) { |
|
|
|
|
list.Add( |
|
|
|
|
delegate (int j) { |
|
|
|
|
for (int k = 0; k < i; k += j) { |
|
|
|
|
list.Add((Action<int>)delegate(int j) { |
|
|
|
|
for (int l = 0; l < i; l += j) { |
|
|
|
|
Console.WriteLine(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
@ -177,8 +169,7 @@ public static class DelegateConstruction
@@ -177,8 +169,7 @@ public static class DelegateConstruction
|
|
|
|
|
{ |
|
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
|
|
for (int k = 0; k < 10; k++) { |
|
|
|
|
list.Add( |
|
|
|
|
delegate(int i) { |
|
|
|
|
list.Add((Action<int>)delegate(int i) { |
|
|
|
|
Console.WriteLine(i); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
@ -186,7 +177,7 @@ public static class DelegateConstruction
@@ -186,7 +177,7 @@ public static class DelegateConstruction
|
|
|
|
|
|
|
|
|
|
public static Action<int> NameConflict3(int i) |
|
|
|
|
{ |
|
|
|
|
return delegate(int j) { |
|
|
|
|
return (Action<int>)delegate(int j) { |
|
|
|
|
for (int k = 0; k < j; k++) { |
|
|
|
|
Console.WriteLine(k); |
|
|
|
|
} |
|
|
|
@ -195,11 +186,11 @@ public static class DelegateConstruction
@@ -195,11 +186,11 @@ public static class DelegateConstruction
|
|
|
|
|
|
|
|
|
|
public static Func<int, Func<int, int>> CurriedAddition(int a) |
|
|
|
|
{ |
|
|
|
|
return b => c => a + b + c; |
|
|
|
|
return (Func<int, Func<int, int>>)((int b) => (Func<int, int>)((int c) => a + b + c)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Func<int, Func<int, Func<int, int>>> CurriedAddition2(int a) |
|
|
|
|
{ |
|
|
|
|
return b => c => d => a + b + c + d; |
|
|
|
|
return (Func<int, Func<int, Func<int, int>>>)((int b) => (Func<int, Func<int, int>>)((int c) => (Func<int, int>)((int d) => a + b + c + d))); |
|
|
|
|
} |
|
|
|
|
} |