Comparison Between Properties and Indexers (C# Programming Guide)
Indexers are like properties. Except for the differences shown in the following table, all the rules that are defined for property accessors apply to indexer accessors also.
Property | Indexer |
---|---|
Allows methods to be called as if they were public data members. | Allows elements of an internal collection of an object to be accessed by using array notation on the object itself. |
Accessed through a simple name. | Accessed through an index. |
Can be a static or an instance member. | Must be an instance member. |
A get accessor of a property has no parameters. | A get accessor of an indexer has the same formal parameter list as the indexer. |
A set accessor of a property contains the implicit value parameter. |
A set accessor of an indexer has the same formal parameter list as the indexer, and also to the value parameter. |
Supports shortened syntax with Auto-Implemented Properties. | Supports expression bodied members for get only indexers. |
See also
Cộng tác với chúng tôi trên GitHub
Bạn có thể tìm thấy nguồn cho nội dung này trên GitHub, nơi bạn cũng có thể tạo và xem lại các vấn đề và yêu cầu kéo. Để biết thêm thông tin, hãy xem hướng dẫn dành cho người đóng góp của chúng tôi.
Phản hồi
https://aka.ms/ContentUserFeedback.
Sắp ra mắt: Trong năm 2024, chúng tôi sẽ dần gỡ bỏ Sự cố với GitHub dưới dạng cơ chế phản hồi cho nội dung và thay thế bằng hệ thống phản hồi mới. Để biết thêm thông tin, hãy xem:Gửi và xem ý kiến phản hồi dành cho