Addison Wesley Essential C Sharp_4
Số trang: 98
Loại file: pdf
Dung lượng: 1.81 MB
Lượt xem: 14
Lượt tải: 0
Xem trước 10 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Kết quả là không sao chép lại từ đống để ngăn xếp xảy ra. Thay vào đó, dữ liệu heap được sửa đổi đã sẵn sàng cho thu gom rác thải trong khi các dữ liệu trong góc vẫn chưa sửa đổi
Nội dung trích xuất từ tài liệu:
Addison Wesley Essential C Sharp_4 345 B oxingThe result is that no copy back from the heap to the stack occurs. Instead,the modified heap data is ready for garbage collection while the data inangle remains unmodified. In the last case, the cast to IAngle occurs with the data on the heapalready, so no copy occurs. MoveTo() updates the _Hours value and thecode behaves as desired. ADVANCED TOPICUnboxing AvoidedAs discussed earlier, the unboxing instruction does not include the copyback to the stack. Although some languages support the ability to accessvalue types on the heap directly, this is possible in C# only when the valuetype is accessed as a field on a reference type. Since interfaces are referencetypes, unboxing and copying can be avoided, when accessing the boxedvalue via its interface. When you call an interface method on a value type, the instance mustbe a variable because the method might mutate the value. Since unboxingproduces a managed address, the runtime has a storage location and hencea variable. As a result, the runtime simply passes that managed address onan interface and no unboxing operation is necessary. Listing 8.7 added an interface implementation to the Angle struct. List-ing 8.8 uses the interface to avoid unboxing.Listing 8.8: Avoiding Unboxing and Copying int number; object thing; number = 42; // Boxing thing = number; // No unbox instruction. string text = ((IFormattable)thing).ToString( X, null); Console.WriteLine(text); Interfaces are reference types anyway, so calling an interface memberdoes not even require unboxing. Furthermore, calling a struct’sToString() method (that overrides object’s ToString() method) does not From the Library of Wow! eBook346 C hapter 8: Value Types require an unbox. When compiling, it is clear that a struct’s overriding ToString() method will always be called because all value types are sealed. The result is that the C# compiler can instruct a direct call to the method without unboxing. Enums Compare the two code snippets shown in Listing 8.9. Listing 8.9: Comparing an Integer Switch to an Enum Switch int connectionState; // ... switch (connectionState) { case 0: // ... break; case 1: // ... break; case 2: // ... break; case 3: // ... break; } ConnectionState connectionState; // ... switch (connectionState) { case ConnectionState.Connected: // ... break; case ConnectionState.Connecting: // ... break; case ConnectionState.Disconnected: // ... break; case ConnectionState.Disconnecting: // ... break; } From the Library of Wow! eBook 347 E numsObviously, the difference in terms of readability is tremendous because inthe second snippet, the cases are self-documenting to some degree. How-ever, the performance at runtime is identical. To achieve this, the secondsnippet uses enum values in each case statement. An enum is a type that the developer can define. The key characteristicof an enum is that it identifies a compile-time-defined set of possible val-ues, each value referred to by name, making the code easier to read. Youdefine an enum using a style similar to that for a class, as Listing 8.10shows.Listing 8.10: Defining an Enum enum ConnectionState { Disconnected, Connecting, Connected, Disconnecting } NOTE An enum is helpful even for Boolean parameters. For example, a method call such as SetState(true) is less readable than SetState (DeviceState.On). You refer to an enum value by prefixing it with the enum name; to refer tothe Connected value, for example, you use ConnectionState.Connected. Youshould not use the enum names within the enum value name, to avoid theredundancy of something such as ConnectionState.ConnectionStateCon-nected. By convention, the enum name itself should be singular, unless theenums are ...
Nội dung trích xuất từ tài liệu:
Addison Wesley Essential C Sharp_4 345 B oxingThe result is that no copy back from the heap to the stack occurs. Instead,the modified heap data is ready for garbage collection while the data inangle remains unmodified. In the last case, the cast to IAngle occurs with the data on the heapalready, so no copy occurs. MoveTo() updates the _Hours value and thecode behaves as desired. ADVANCED TOPICUnboxing AvoidedAs discussed earlier, the unboxing instruction does not include the copyback to the stack. Although some languages support the ability to accessvalue types on the heap directly, this is possible in C# only when the valuetype is accessed as a field on a reference type. Since interfaces are referencetypes, unboxing and copying can be avoided, when accessing the boxedvalue via its interface. When you call an interface method on a value type, the instance mustbe a variable because the method might mutate the value. Since unboxingproduces a managed address, the runtime has a storage location and hencea variable. As a result, the runtime simply passes that managed address onan interface and no unboxing operation is necessary. Listing 8.7 added an interface implementation to the Angle struct. List-ing 8.8 uses the interface to avoid unboxing.Listing 8.8: Avoiding Unboxing and Copying int number; object thing; number = 42; // Boxing thing = number; // No unbox instruction. string text = ((IFormattable)thing).ToString( X, null); Console.WriteLine(text); Interfaces are reference types anyway, so calling an interface memberdoes not even require unboxing. Furthermore, calling a struct’sToString() method (that overrides object’s ToString() method) does not From the Library of Wow! eBook346 C hapter 8: Value Types require an unbox. When compiling, it is clear that a struct’s overriding ToString() method will always be called because all value types are sealed. The result is that the C# compiler can instruct a direct call to the method without unboxing. Enums Compare the two code snippets shown in Listing 8.9. Listing 8.9: Comparing an Integer Switch to an Enum Switch int connectionState; // ... switch (connectionState) { case 0: // ... break; case 1: // ... break; case 2: // ... break; case 3: // ... break; } ConnectionState connectionState; // ... switch (connectionState) { case ConnectionState.Connected: // ... break; case ConnectionState.Connecting: // ... break; case ConnectionState.Disconnected: // ... break; case ConnectionState.Disconnecting: // ... break; } From the Library of Wow! eBook 347 E numsObviously, the difference in terms of readability is tremendous because inthe second snippet, the cases are self-documenting to some degree. How-ever, the performance at runtime is identical. To achieve this, the secondsnippet uses enum values in each case statement. An enum is a type that the developer can define. The key characteristicof an enum is that it identifies a compile-time-defined set of possible val-ues, each value referred to by name, making the code easier to read. Youdefine an enum using a style similar to that for a class, as Listing 8.10shows.Listing 8.10: Defining an Enum enum ConnectionState { Disconnected, Connecting, Connected, Disconnecting } NOTE An enum is helpful even for Boolean parameters. For example, a method call such as SetState(true) is less readable than SetState (DeviceState.On). You refer to an enum value by prefixing it with the enum name; to refer tothe Connected value, for example, you use ConnectionState.Connected. Youshould not use the enum names within the enum value name, to avoid theredundancy of something such as ConnectionState.ConnectionStateCon-nected. By convention, the enum name itself should be singular, unless theenums are ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhGợi ý tài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 318 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 305 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 291 0 0 -
70 trang 251 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 238 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 234 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 214 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 208 0 0 -
Tổng hợp 30 lỗi thương gặp cho những bạn mới sử dụng máy tính
9 trang 205 0 0 -
Phần III: Xử lý sự cố Màn hình xanh
3 trang 204 0 0