TAILIEUCHUNG - ebook Addison -Essential CSharp 4.0_9

Nếu dữ liệu là một loại biến không được quản lý nhưng không phải là cố định, sau đó sử dụng câu lệnh cố định để sửa chữa một biến thể di chuyển. | Pointers and Addresses 835 If the data is an unmanaged variable type but is not fixed then use the fixed statement to fix a moveable variable. Fixing Data To retrieve the address of a moveable data item it is necessary to fix or pin the data as demonstrated in Listing . Listing Fixed Statement byte bytes new byte 24 fixed byte pData bytes 0 pData bytes also allowed . Within the code block of a fixed statement the assigned data will not move. In this example bytes will remain at the same address at least until the end of the fixed statement. The fixed statement requires the declaration of the pointer variable within its scope. This avoids accessing the variable outside the fixed statement when the data is no longer fixed. However it is the programmer s responsibility to ensure that he doesn t assign the pointer to another variable that survives beyond the scope of the fixed statement possibly in an API call for example. Similarly using ref or out parameters will be problematic for data that will not survive beyond the method call. Since a string is an invalid referent type it would appear invalid to define pointers to strings. However as in C internally a string is a pointer to the first character of an array of characters and it is possible to declare pointers to characters using char . Therefore C allows declaring a pointer of type char and assigning it to a string within a fixed statement. The fixed statement prevents the movement of the string during the life of the pointer. Similarly it allows any moveable type that supports an implicit conversion to a pointer of another type given a fixed statement. You can replace the verbose assignment of bytes 0 with the abbreviated bytes as shown in Listing . Chapter 20 Platform Interoperability and Unsafe Code Listing Fixed Statement without Address or Array Indexer byte bytes new byte 24 fixed byte pData bytes . Depending on the frequency and time to execute fixed statements have the potential to .

TỪ KHÓA LIÊN QUAN
Đã 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.