Essential C# 5.0 by Mark Michaelis & Eric Lippert

Essential C# 5.0 by Mark Michaelis & Eric Lippert

Author:Mark Michaelis & Eric Lippert [Michaelis, Mark & Lippert, Eric]
Language: eng
Format: epub
Publisher: Addison-Wesley
Published: 2013-08-26T22:00:00+00:00


* * *

class DelegateSample

{

// ...

static void Main(string[] args)

{

int i;

int[] items = new int[5];

for (i=0; i<items.Length; i++)

{

Console.Write("Enter an integer:");

items[i] = int.Parse(Console.ReadLine());

}

BubbleSort(items,

DelegateSample.__AnonymousMethod_00000000);

for (i = 0; i < items.Length; i++)

{

Console.WriteLine(items[i]);

}

}

private static bool __AnonymousMethod_00000000(

int first, int second)

{

return first < second;

}

}

* * *

In this example, the compiler transforms an anonymous function into a separately declared static method that is then instantiated as a delegate and passed as a parameter. Unsurprisingly, the compiler generates code that looks remarkably like the original code in Listing 12.7 that the anonymous function syntax was intended to streamline. However, the code transformation performed by the compiler can be considerably more complex than merely rewriting the anonymous function as a static method if “outer variables” are involved.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.