CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 620

620
Chương 16: Các giao diện và mẫu thông dụng

this.name = name;

this.circulation = circulation;

}

// Khai báo một thuộc tính chỉ-đọc, trả về một thể hiện của

// AscendingCirculationComparer.

public static IComparer CirculationSorter{

get { return new AscendingCirculationComparer(); }

}

public override string ToString() {

return string.Format("{0}: Circulation = {1}", name,

circulation);

}

// Phương thức CompareTo so sánh hai đối tượng Newspaper dựa trên

// phép so sánh trường name (không phân biệt chữ hoa-thường).

public int CompareTo(object obj) {

// Một đối tượng luôn được coi như lớn hơn null.

if (obj == null) return 1;

// Trường hợp đối tượng kia là một tham chiếu đến đối tượng này.

if (obj == this) return 0;

// Ép đối tượng kia về Newspaper.

Newspaper other = obj as Newspaper;

// Nếu "other" là null, nó không phải là một thể hiện của

// Newspaper. Trong trường hợp này, CompareTo phải ném

// ngoại lệ System.ArgumentException.

if (other == null) {

throw new ArgumentException("Invalid object type", "obj");

} else {