TAILIEUCHUNG - Addison Essential Csharp_4

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. Trong trường hợp cuối cùng, dàn diễn viên để IAngle xảy ra với các dữ liệu trên đống đã được, do đó, bản sao không xảy ra. | Boxing 345 The 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 in angle remains unmodified. In the last case the cast to lAngle occurs with the data on the heap already so no copy occurs. MoveTo updates the -Hours value and the code behaves as desired. ADVANCED TOPIC Unboxing Avoided As discussed earlier the unboxing instruction does not include the copy back to the stack. Although some languages support the ability to access value types on the heap directly this is possible in C only when the value type is accessed as a field on a reference type. Since interfaces are reference types unboxing and copying can be avoided when accessing the boxed value via its interface. When you call an interface method on a value type the instance must be a variable because the method might mutate the value. Since unboxing produces a managed address the runtime has a storage location and hence a variable. As a result the runtime simply passes that managed address on an interface and no unboxing operation is necessary. Listing added an interface implementation to the Angle struct. Listing uses the interface to avoid unboxing. Listing Avoiding Unboxing and Copying int number object thing number 42 Boxing thing number No unbox instruction. string text IFormattable thing .ToString X null text Interfaces are reference types anyway so calling an interface member does not even require unboxing. Furthermore calling a struct s ToString method that overrides object s ToString method does not Chapter 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 . Listing Comparing an Integer Switch to an Enum Switch int .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.