我正在学习C#和Python的一些新东西。结果发现这两种语言都支持嵌套方法(C#有点)。
Python:
def MyMethod():
print 'Hello from a method.'
def MyInnerMethod():
print 'Hello from a nested method.'
MyInnerMethod()
C#(使用.NET 3.5的新功能):
static void Main()
{
Console.WriteLine("Hello from main.");
Func<int, int> NestedMethod =
(x) =>
{
Console.WriteLine("In nested method. Value of x is {0}.", x);
return x;
};
int result = NestedMethod(3);
}
为什么嵌套方法如此重要?它们有哪些作用呢?
**该 C# 代码尚未经过测试。如果无法编译,请随意编辑。