TAILIEUCHUNG - Addison Essential Csharp_6

Hai lỗi biên dịch kết quả khẳng định một thực tế rằng các loại không tương thích, vì vậy họ sẽ không thành công chuyển đổi từ một đến khác. Các lỗi biên dịch thứ ba là do tái chỉ định của tài sản Tiêu đề. Các loại vô danh là không thay đổi | Anonymous Types and Implicitly Typed Local Variables 541 Listing Type Safety and Immutability of Anonymous Types class Program static void Main var patentl new Title Bifocals YearOfPublication 1784 var patent2 new YearOfPublication 1877 Title Phonograph var patent3 new Year ERROR Cannot implicitly convert type AnonymousType 1 to AnonymousType 2 patent1 patent2 ERROR Cannot implicitly convert type AnonymousType 3 to AnonymousType 2 patent1 patent3 ERROR Property or indexer AnonymousType cannot be assigned to -- it is read only Swiss Cheese The resultant two compile errors assert the fact that the types are not compatible so they will not successfully convert from one to the other. The third compile error is caused by the reassignment of the Title property. Anonymous types are immutable so it is a compile error to change a property on an anonymous type once it has been instantiated. Although not shown in Listing it is not possible to declare a method with an implicit data type parameter var . Therefore instances Chapter 14 Collection Interfaces with Standard Query Operators of anonymous types can only be passed outside the method in which they are created in only two ways. First if the method parameter is of type object the anonymous type instance may pass outside the method because the anonymous type will convert implicitly. A second way is to use method type inference whereby the anonymous type instance is passed as a method type parameter that the compiler can successfully infer. Calling void Method T T parameter using Function patentl therefore would succeed although the available operations on parameter within Function are limited to those supported by object. In spite of the fact that C allows anonymous types such as the ones shown in Listing it is generally not recommended that you define them in this way. Anonymous types provide critical functionality with C support for .

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.