Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Praise for C# 2.0: Practical Guide for Programmers 2005 phần 3 docx
MIỄN PHÍ
Số trang
26
Kích thước
451.3 KB
Định dạng
PDF
Lượt xem
1034

Praise for C# 2.0: Practical Guide for Programmers 2005 phần 3 docx

Nội dung xem thử

Mô tả chi tiết

36 Chapter 3: Class Members and Class Reuse ■

Console.WriteLine("|{0:P}|{1:N}|", 1.23, 1.23);

Console.WriteLine("|{0:X}|{1:X5}|{2,5:X}|{3,-5:X}|", 255, 255, 255, 255);

Console.WriteLine("|{0:#.00}|{1:0.00}|{2,5:0.00}|{3,-5:0.00}|",

.23, .23, .23, .23);

}

}

Output:

|$1.23|($1.23)|

|123|-0123|

|1.23|1.2300|

|1.230000E+000|1.23|

|123.00 %|1.23|

|FF|000FF| FF|FF |

|.23|0.23| 0.23|0.23 |

3.1.4 Declaring Destructors

The garbage collector in C# is an automatic memory management scheme that scans for

objects that are no longer referenced and are therefore eligible for destruction. Hence,

memory allocated to an object is recouped automatically by a garbage collector when

the object is no longer accessible (or reachable). Although the garbage collector may be

invoked directly using the GC.Collect method, this practice sidesteps the heuristics and

complex algorithms that are used to optimize system performance. Unless there are com￾pelling reasons to do otherwise, garbage collection is best left to the system rather than

the programmer. It is safer, easier, and more efficient.

However, an object may acquire resources that are unknown to the garbage collector,

such as peripheral devices and database connections. These resources are the respon￾sibility of the object itself and, therefore, the logic to release these resources must be

Type of Format Meaning

c or C Currency

d or D Decimal

e or E Scientific with “e" or “E" (6 digits)

f or F Fixed-point (12 digits)

g or G General (the most compact between E and F)

n or N Number

p or P Percent

x or X Hexadecimal

Table 3.1: Numeric format types.

■ 3.2 Parameter Passing 37

implemented in a special method called a destructor. Although an object may be instan￾tiated in any number of ways, at most one destructor is declared per class. A destructor,

as shown here for the class Id, where Id is preceded by a tilde (˜), cannot be inherited,

overloaded, or explicitly invoked.

public class Id {

˜Id () { /* release of resources */ }

}

Instead, each destructor is invoked automatically but non-deterministically at the end

of a program or by the garbage collector itself. To ensure that a destructor is invoked

immediately once an object is no longer referenced, the IDisposable .NET design pattern Tip

should be used as described in Section 9.1. Such a destructor is also called a finalizer in

the .NET context.

3.2 Parameter Passing

As described earlier in the chapter, each method in C# has an optional sequence of formal

parameters. Each formal parameter, in turn, represents a special kind of local variable

that specifies the type of argument that must be passed to the given method. Like other

local variables, formal parameters are allocated on the stack when a method is invoked

and are deallocated when the method completes its execution. Therefore, the lifetime of a

parameter and the lifetime of its method are synonymous. Finally, arguments are passed

to formal parameters in one of two ways: by value or by reference. These ways are

explored in greater detail in the following two sections.

3.2.1 Passing Arguments by Value

When an argument is passed by value, the formal parameter is initialized to a copy of the

actual argument. Therefore, the actual argument itself cannot be modified by the invoked

method. In the following example, an integer variable p is passed by value to a formal

parameter of the same name. Although the formal parameter may change its local copy

of p, the value of p in the main program retains its original value after the invocation of

ParambyValue.

using System;

class ParambyValue {

static void Fct(int p) {

Console.WriteLine("In Fct: p = {0}", ++p);

}

static void Main() {

int p = 1;

Console.WriteLine("Before: p = {0}", p);

Fct(p);

Tải ngay đi em, còn do dự, trời tối mất!