Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

การได้ Enumerator วนลูป Collection ใน C#

Collection<T> (ใน System.Collections.ObjectModel) เป็น base class สำหรับสร้างคอลเลกชัน custom มีเมธอด GetEnumerator() คืน IEnumerator<T> สำหรับวนลูปผ่านสมาชิกทั้งหมด

ตัวอย่างโค้ดที่ 1: สตริง

using System;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection col = new Collection();
        col.Add("Andy");
        col.Add("Kevin");
        col.Add("John");
        col.Add("Kevin"); // Duplicate allowed
        col.Add("Mary");
        col.Add("Katie");
        col.Add("Barry");
        col.Add("Nathan");
        col.Add("Mark");

        Console.WriteLine("Count of elements = " + col.Count);
        Console.WriteLine("Iterating through the collection...");
        var enumerator = col.GetEnumerator();
        while (enumerator.MoveNext())
        {
            Console.WriteLine(enumerator.Current);
        }
    }
}

ผลลัพธ์

Count of elements = 9
Iterating through the collection...
Andy
Kevin
John
Kevin
Mary
Katie
Barry
Nathan
Mark

ตัวอย่างโค้ดที่ 2: จำนวนเต็ม

using System;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection col = new Collection();
        col.Add(100);
        col.Add(200);
        col.Add(300);
        col.Add(400);
        col.Add(500);

        Console.WriteLine("Count of elements = " + col.Count);
        Console.WriteLine("Iterating through the collection...");
        var enumerator = col.GetEnumerator();
        while (enumerator.MoveNext())
        {
            Console.WriteLine(enumerator.Current);
        }
    }
}

ผลลัพธ์

Count of elements = 5
Iterating through the collection...
100
200
300
400
500

คุณสมบัติของ Collection<T>

Memberคำอธิบาย
Countจำนวนสมาชิก
Add(T)เพิ่มที่ท้าย
Insert(int index, T)แทรกที่ตำแหน่ง
Remove(T) / RemoveAt(int index)ลบตามค่า/ดัชนี
Clear()ลบทั้งหมด
Contains(T)ตรวจสอบมีอยู่
IndexOf(T)หาดัชนีแรก (-1 หากไม่พบ)
[int index] (indexer)ได้/ตั้งค่า ด้วยดัชนี
GetEnumerator()ได้ IEnumerator<T>
Items (protected)IList<T> ภายใน - สำหรับ subclass

Virtual Methods สำหรับ Customization (Override ได้)

  • InsertItem(int index, T item)
  • SetItem(int index, T item)
  • RemoveItem(int index)
  • ClearItems()

เหล่านี้ถูกเรียกโดย public methods (Add, Insert, Remove, Clear) - ใช้สร้างคอลเลกชันที่มี validation, notification, logging ฯลฯ

Collection<T> vs List<T> vs ObservableCollection<T>

คุณสมบัติCollection<T>List<T>ObservableCollection<T>
Extensibilityสูง (virtual methods)ต่ำ (sealed methods)กลาง
INotifyCollectionChangedไม่มีไม่มีมี (WPF/UI binding)
Performanceดีดีที่สุดดี
Use CaseCustom collection baseGeneral purposeUI Data Binding

การใช้งานจริง: Custom Collection


public class ValidatedCollection<T> : Collection<T>
{
    protected override void InsertItem(int index, T item)
    {
        if (item == null) throw new ArgumentNullException(nameof(item));
        base.InsertItem(index, item);
    }
    
    protected override void SetItem(int index, T item)
    {
        if (item == null) throw new ArgumentNullException(nameof(item));
        base.SetItem(index, item);
    }
}