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», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

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

Интервал:

Закладка:

Сделать

using System.Text;

namespace MathUtil {

public class Utils {

public int Fibonacci(int num) {

if (num <= 1) return 2; //---should return 1; error on purpose---

return Fibonacci(num - 1) + Fibonacci(num - 2);

}

}

}

This Utilsclass contains a method called Fibonacci(), which returns the n thnumber in the Fibonacci sequence (note that I have purposely injected an error into the code so that I can later show you how the application can be easily updated by replacing the DLL). Figure 15-5 shows the first 20 numbers in the correct Fibonacci sequence.

Figure 155 Build the Class Library project rightclick on the projects - фото 215

Figure 15-5

Build the Class Library project (right-click on the project's name in Solution Explorer, and select Build) so that it will compile into a DLL — MathUtil.dll.

Add a Windows Application project to the current solution, and name it WindowsApp-Util. This application will use the Fibonacci()method defined in MathUtil.dll. Because the MathUtil.dllassembly is created in the same solution as the Windows project, you can find it in the Projects tab of the Add Reference dialog (see Figure 15-6). Select the assembly, and click OK.

Figure 156 The MathUtildllassembly will now be added to the project - фото 216

Figure 15-6

The MathUtil.dllassembly will now be added to the project. Observe that the Copy Localproperty for the MathUtil.dllassembly is set to True(see Figure 15-7). This means that a copy of the assembly will be placed in the project's output directory (that is, the bin\Debug folder).

Figure 157 When you add a reference to one of the classes in the NET class - фото 217

Figure 15-7

When you add a reference to one of the classes in the .NET class library, the Copy Local property for the added assembly will be set to False. That's because the .NET assembly is in the Global Assembly Cache (GAC), and all computers with the .NET Framework installed have the GAC. The GAC is discussed later in this chapter.

Switch to the code-behind of the default Form1and code the following statements:

namespace WindowsApp_Util {

public partial class Form1 : Form {

public Form1() {

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e) {

CallUtil();

}

private void CallUtil() {

MathUtil.Utils util = new MathUtil.Utils();

MessageBox.Show(util.Fibonacci(7).ToString());

}

}

}

Set a breakpoint at the CallMathUtil()method (see Figure 15-8).

Figure 158 Rightclick on the WindowsAppUtilproject name in Solution - фото 218

Figure 15-8

Right-click on the WindowsApp-Utilproject name in Solution Explorer, and select Start as Startup Project. Press F5 to debug the application. When the application stops at the breakpoint, view the modules loaded into memory by selecting Debug→Windows→Modules (see Figure 15-9).

Figure 159 Observe that MathUtildlllibrary has not been loaded yet Press - фото 219

Figure 15-9

Observe that MathUtil.dlllibrary has not been loaded yet. Press F11 to step into the CallMathUtil()function (see Figure 15-10). The MathUtil.dlllibrary is now loaded into memory.

Figure 1510 Press F5 to continue the execution You should see a message box - фото 220

Figure 15-10

Press F5 to continue the execution. You should see a message box displaying the value 42. In the bin\Debug folder of the Windows application project, you will find the EXE assembly as well as the DLL assembly (see Figure 15-11).

Figure 1511 Updating the DLL The Fibonaccimethod defined in the - фото 221

Figure 15-11

Updating the DLL

The Fibonacci()method defined in the MathUtilproject contains a bug. When numis less than or equal to 1, the method should return 1 and not 2. In the real world, the application and the DLL may already been deployed to the end user's computer. To fix this bug, you simply need to modify the Utilsclass, recompile it, and then update the user's computer with the new DLL:

namespace MathUtil {

public class Utils {

public int Fibonacci(int num) {

if (num <= 1) return 1; //---fixed!---

return Fibonacci(num - 1) + Fibonacci(num - 2);

}

}

}

Copy the recompiled MathUtil.dllfrom the bin\Debug folder of the MathUtilproject, and overwrite the original MathUtil.dlllocated in the bin\Debug folder of the Windows project. When the application runs again, it will display the correct value, 21 (previously it displayed 42).

Because the MathUtil.dllassembly is not digitally signed, a hacker could replace this assembly with one that contains malicious code, and the client of this assembly (which is the WindowsApp-Util application in this case) would not know that the assembly has been tampered with. Later in this chapter, you will see how to give the assembly a unique identity using a strong name.

Modules and Assemblies

An application using a library loads it only when necessary — the entire library is loaded into memory during runtime. If the library is large, your application uses up more memory and takes a longer time to load. To solve this problem, you can split an assembly into multiple modules and then compile each individually as a module. The modules can then be compiled into an assembly.

To see how you can use a module instead of an assembly, add a new Class Library project to the solution used in the previous section. Name the Class Library project StringUtil. Populate the default Class1.csfile as follows:

using System.Text.RegularExpressions;

namespace StringUtil {

public class Utils {

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

Интервал:

Закладка:

Сделать

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

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


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

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

x