Wei-Meng Lee - C# 2008 Programmer's Reference

Здесь есть возможность читать онлайн «Wei-Meng Lee - C# 2008 Programmer's Reference» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Город: Indianapolis, Год выпуска: 2009, ISBN: 2009, Издательство: Wiley Publishing, Inc., Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

C# 2008 Programmer's Reference: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «C# 2008 Programmer's Reference»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

C# 2008 Programmers Reference provides a concise and thorough reference on all aspects of the language. Each chapter contains detailed code samples that provide a quick and easy way to understand the key concepts covered.

C# 2008 Programmer's Reference — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «C# 2008 Programmer's Reference», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

MethodDelegate del =

(MethodDelegate)((AsyncResult)ar).AsyncDelegate;

//---get the result---

int result = del.EndInvoke(ar);

Console.WriteLine("Result of addition is: " + result);

}

Within the ResultCallback()method, you first obtain the delegate to the AddTwoNumbers()method by using the AsyncDelegateproperty, which returns the delegate on which the asynchronous call was invoked. You then obtain the result of the asynchronous call by using the EndInvoke()method, passing it the IAsyncResultvariable ( ar).

Finally, to demonstrate the asynchronous calling of the AddTwoNumbers()method, you can insert a Sleep()statement to delay the execution (simulating long execution):

static private int AddTwoNumbers(int num1, int num2) {

//---simulate long execution---

System.Threading.Thread.Sleep(5000);

return num1 + num2;

}

Figure 7-3 shows the output of this program.

Figure 73 When using asynchronous callbacks you can make your program much - фото 123

Figure 7-3

When using asynchronous callbacks, you can make your program much more responsive by executing different parts of the program in different threads.

Chapter 10 discusses more about threading.

Anonymous Methods and Lambda Expressions

Beginning with C# 2.0, you can use a feature known as anonymous methods to define a delegate.

An anonymous method is an "inline" statement or expression that can be used as a delegate parameter. To see how it works, take a look at the following example:

class Program {

delegate void MethodsDelegate(string Message);

static void Main(string[] args) {

MethodsDelegate method = Method1;

//---call the delegated method---

method("Using delegate.");

Console.ReadLine();

}

static private void Method1(string Message) {

Console.WriteLine(Message);

}

}

Instead of defining a separate method and then using a delegate variable to point to it, you can shorten the code using an anonymous method:

class Program {

delegate void MethodsDelegate(string Message);

static void Main(string[] args) {

MethodsDelegate method = delegate(string Message) {

Console.WriteLine(Message);

};

//---call the delegated method---

method("Using anonymous method.");

Console.ReadLine();

}

}

In this expression, the methoddelegate is an anonymous method:

MethodsDelegate method = delegate(string Message) {

Console.WriteLine(Message);

};

Anonymous methods eliminate the need to define a separate method when using delegates. This is useful if your delegated method contains a few simple statements and is not used by other code because you reduce the coding overhead in instantiating delegates by not having to create a separate method.

In C# 3.0, anonymous methods can be further shortened using a new feature known as lambda expressions. Lambda expressions are a new feature in .NET 3.5 that provides a more concise, functional syntax for writing anonymous methods.

The preceding code using anonymous methods can be rewritten using a lambda expression:

class Program {

delegate void MethodsDelegate(string Message);

static void Main(string[] args) {

MethodsDelegate method = (Message) => { Console.WriteLine(Message); };

//---call the delegated method---

method("Using Lambda Expression.");

Console.ReadLine();

}

}

Lambda expressions are discussed in more detail in Chapter 14.

Events

One of the most important techniques in computer science that made today's graphical user interface operating systems (such as Windows, Mac OS X, Linux, and so on) possible is event-driven programming. Event-driven programming lets the OS react appropriately to the different clicks made by the user. A typical Windows application has various widgets such as buttons, radio buttons, and checkboxes that can raise events when, say, a user clicks them. The programmer simply needs to write the code to handle that particular event. The nice thing about events is that you do not need to know when these events will be raised — you simply need to provide the implementation for the event handlers that will handle the events and the OS will take care of invoking the necessary event handlers appropriately.

In .NET, events are implemented using delegates. An object that has events is known as a publisher. Objects that subscribe to events (in other words, handle events) are known as subscribers. When an object exposes events, it defines a delegate so that whichever object wants to handle this event will have to provide a function for this delegate. This delegate is known as an event, and the function that handles this delegate is known as an event handler. Events are part and parcel of every Windows application. For example, using Visual Studio 2008 you can create a Windows application containing a Buttoncontrol (see Figure 7-4).

Figure 74 When you doubleclick the Button control an event handler is - фото 124

Figure 7-4

When you double-click the Button control, an event handler is automatically added for you:

public partial class Form1 : Form {

public Form1() {

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e) {

}

}

But how does your application know which event handler is for which event? Turns out that Visual Studio 2008 automatically wires up the event handlers in the code-behind of the form ( FormName.Designer.cs; see Figure 7-5) located in a function called InitializeComponent():

this.button1.Location = new System.Drawing.Point(12, 12);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(75, 23);

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.UseVisualStyleBackColor = true;

this.button1.Click += new System.EventHandler(this.button1_Click);

Figure 75 Notice that the way you wire up an event handler to handle the - фото 125

Figure 7-5

Notice that the way you wire up an event handler to handle the Clickevent is similar to how you assign a method name to a delegate.

Alternatively, you can manually create the event handler for the Clickevent of the Buttoncontrol. In the Form()constructor, type +=after the Clickevent and press the Tab key. Visual Studio 2008 automatically completes the statement (see Figure 7-6).

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «C# 2008 Programmer's Reference»

Представляем Вашему вниманию похожие книги на «C# 2008 Programmer's Reference» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «C# 2008 Programmer's Reference»

Обсуждение, отзывы о книге «C# 2008 Programmer's Reference» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x