What Are Collection Classes?

Bộ sưu tập các lớp học là gì? Mảng là hữu ích, nhưng chúng có những giới hạn của họ. Tuy nhiên, mảng được chỉ có một cách để thu thập các yếu tố của cùng loại. Framework cung cấp một số lớp học cũng thu thập các yếu tố với nhau theo những cách khác chuyên ngành. | What Are Collection Classes Arrays are useful but they have their limitations. However arrays are only one way to collect elements of the same type. Framework provides several classes that also collect elements together in other specialized ways. These are the Collection classes and they live in the namespace and sub-namespaces. The basic collection classes accept hold and return their elements as objects. That is the element type of a collection class is an object. To understand the implications of this it is helpful to contrast an array of int variables int is a value type with an array of objects object is a reference type . Because int is a value type an array of int variables holds its int values directly as shown in the following graphic Now consider the effect when the array is an array of objects. You can still add integer values to this array in fact you can add values of any type to it . When you add an integer value it is automatically boxed and the array element an object reference refers to the boxed copy of the integer value. For a refresher on boxing refer to Chapter 8. This is illustrated in the following graphic Remember that the element type of a collection class is an object. This means that when you insert a value into a collection it is always boxed and when you remove a value from a collection you must unbox it by using a cast. The following sections provide a very quick overview of four of the most useful Collection classes. Refer to the .NET Framework documentation for more details on each class. NOTE In fact there are Collection classes that don t always use object as their element type and that can hold value types as well as references but you need to know a bit more about C before we can talk about them. You will meet these Collection classes in Chapter 17 Introducing Generics. The ArrayList Class ArrayList is a useful class for shuffling elements around in an array. There are certain occasions when an ordinary

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
5    58    1    30-04-2024
Đã 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.