namespace testAnony {
class SomeClass {
public delegate void SomeDelegate();
Console.WriteLine("=== BEGIN DELEGATE --------------\n");
del();
Console.WriteLine("\n=== END DELEGATE ----------------\n");
}
}
static void Main(string[] args) {
new SomeClass().InvokeDelegate(CustomWrite);
new SomeClass().InvokeDelegate(AnotherCustomWrite);
Console.WriteLine("是的,当时就是那样");
Console.WriteLine("这就是匿名函数");
});
}
Console.WriteLine("hello nonocast!");
}
Console.WriteLine("good day, sir");
}
}
}
class SomeClass {
public delegate void SomeDelegate();
public void InvokeDelegate(SomeDelegate del) {
Console.WriteLine("=== BEGIN DELEGATE --------------\n");
del();
Console.WriteLine("\n=== END DELEGATE ----------------\n");
}
}
public class Program {
static void Main(string[] args) {
new SomeClass().InvokeDelegate(CustomWrite);
new SomeClass().InvokeDelegate(AnotherCustomWrite);
new SomeClass().InvokeDelegate(delegate() {
Console.WriteLine("是的,当时就是那样");
Console.WriteLine("这就是匿名函数");
});
}
static public void CustomWrite() {
Console.WriteLine("hello nonocast!");
}
static public void AnotherCustomWrite() {
Console.WriteLine("good day, sir");
}
}
}
是的,在代码面前更多的语言都是徒劳的
It's enough..i think
BTW:
可以做一个横向关联,那就是ruby的block,原理是一样的,都属于函数式编程的范畴。
def execute
yield
end
puts "it's block!"
end
yield
end
execute
doputs "it's block!"
end
再一个大家比较熟悉的就是java中的匿名类
interface SomeInterface {
public void SomeMethod();
} public class Program {
static public void main(String[] args) {
new SomeInterface() {
public void SomeMethod() {
System.out.println("Hello World");
}
}.SomeMethod();
}
}
public void SomeMethod();
} public class Program {
static public void main(String[] args) {
new SomeInterface() {
public void SomeMethod() {
System.out.println("Hello World");
}
}.SomeMethod();
}
}