03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Extension Methods ❘ 751<br />

An async lambda is a lambda expression with the async keyword added, so the program can execute<br />

it asynchronously. For example, the following code adds an asynchronous statement lambda to the<br />

countButton control’s Click event.<br />

private void Form1_Load(object sender, EventArgs e)<br />

{<br />

countButton.Click += async (button, args) =><br />

{<br />

for (int i = 0; i < 5; i++)<br />

{<br />

Console.WriteLine(i);<br />

await System.Threading.Tasks.Task.Delay(1000);<br />

}<br />

};<br />

}<br />

Extension Methods<br />

To make an extension method, place a static method in a static class. Add the keyword this<br />

before the method’s first parameter and give that parameter the type that you want to extend. For<br />

example, the following code defines a RemoveNonLetters string extension method.<br />

public static class StringExtensions<br />

{<br />

public static string RemoveNonLetters(this string text)<br />

{<br />

string result = "";<br />

foreach (char ch in text)<br />

if (((ch >= 'a') && (ch = 'A') && (ch

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!