|
|
@ -3,43 +3,82 @@ |
|
|
|
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
using System.Linq; |
|
|
|
|
|
|
|
|
|
|
|
public static class DelegateConstruction |
|
|
|
public static class DelegateConstruction |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
class InstanceTests |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public Action CaptureOfThis() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return delegate { |
|
|
|
|
|
|
|
CaptureOfThis(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Action CaptureOfThisAndParameter(int a) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return delegate { |
|
|
|
|
|
|
|
CaptureOfThisAndParameter(a); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Action CaptureOfThisAndParameterInForEach(int a) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach (var item in Enumerable.Empty<int>()) { |
|
|
|
|
|
|
|
return delegate { |
|
|
|
|
|
|
|
CaptureOfThisAndParameter(item + a); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Action CaptureOfThisAndParameterInForEachWithItemCopy(int a) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach (var item in Enumerable.Empty<int>()) { |
|
|
|
|
|
|
|
int copyOfItem = item; |
|
|
|
|
|
|
|
return delegate { |
|
|
|
|
|
|
|
CaptureOfThisAndParameter(item + a + copyOfItem); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void Test(this string a) |
|
|
|
public static void Test(this string a) |
|
|
|
{ |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Action<string> ExtensionMethodUnbound() |
|
|
|
public static Action<string> ExtensionMethodUnbound() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return new Action<string>(DelegateConstruction.Test); |
|
|
|
return new Action<string>(DelegateConstruction.Test); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Action ExtensionMethodBound() |
|
|
|
public static Action ExtensionMethodBound() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return new Action("abc".Test); |
|
|
|
return new Action("abc".Test); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Action ExtensionMethodBoundOnNull() |
|
|
|
public static Action ExtensionMethodBoundOnNull() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return new Action(((string)null).Test); |
|
|
|
return new Action(((string)null).Test); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static object StaticMethod() |
|
|
|
public static object StaticMethod() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return new Func<Action>(DelegateConstruction.ExtensionMethodBound); |
|
|
|
return new Func<Action>(DelegateConstruction.ExtensionMethodBound); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static object InstanceMethod() |
|
|
|
public static object InstanceMethod() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return new Func<string>("hello".ToUpper); |
|
|
|
return new Func<string>("hello".ToUpper); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static object InstanceMethodOnNull() |
|
|
|
public static object InstanceMethodOnNull() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return new Func<string>(((string)null).ToUpper); |
|
|
|
return new Func<string>(((string)null).ToUpper); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static List<Action<int>> AnonymousMethodStoreWithinLoop() |
|
|
|
public static List<Action<int>> AnonymousMethodStoreWithinLoop() |
|
|
|
{ |
|
|
|
{ |
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
@ -54,7 +93,7 @@ public static class DelegateConstruction |
|
|
|
} |
|
|
|
} |
|
|
|
return list; |
|
|
|
return list; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static List<Action<int>> AnonymousMethodStoreOutsideLoop() |
|
|
|
public static List<Action<int>> AnonymousMethodStoreOutsideLoop() |
|
|
|
{ |
|
|
|
{ |
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
@ -69,7 +108,7 @@ public static class DelegateConstruction |
|
|
|
} |
|
|
|
} |
|
|
|
return list; |
|
|
|
return list; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Action StaticAnonymousMethodNoClosure() |
|
|
|
public static Action StaticAnonymousMethodNoClosure() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return delegate |
|
|
|
return delegate |
|
|
@ -77,7 +116,7 @@ public static class DelegateConstruction |
|
|
|
Console.WriteLine(); |
|
|
|
Console.WriteLine(); |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void NameConflict() |
|
|
|
public static void NameConflict() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// i is captured variable,
|
|
|
|
// i is captured variable,
|
|
|
@ -98,7 +137,7 @@ public static class DelegateConstruction |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void NameConflict2(int j) |
|
|
|
public static void NameConflict2(int j) |
|
|
|
{ |
|
|
|
{ |
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
|
List<Action<int>> list = new List<Action<int>>(); |
|
|
@ -109,7 +148,7 @@ public static class DelegateConstruction |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Action<int> NameConflict3(int i) |
|
|
|
public static Action<int> NameConflict3(int i) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return delegate(int j) { |
|
|
|
return delegate(int j) { |
|
|
|