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

รับตัวแปรระบุ (enumerator) สำหรับวนลูป StringCollection ใน C#

บทความนี้สาธิตวิธีรับ StringEnumerator เพื่อวนลูปผ่าน StringCollection ใน C#

ตัวอย่างที่ 1: เพิ่มข้อมูลจากอาเรย์และวนลูปด้วย StringEnumerator

using System;
using System.Collections.Specialized;

public class Demo
{
    public static void Main()
    {
        StringCollection stringCol = new StringCollection();
        String[] arr = new String[] { "100", "200", "300", "400", "500" };

        Console.WriteLine("Array elements...");
        foreach (string res in arr)
        {
            Console.WriteLine(res);
        }

        stringCol.AddRange(arr);

        Console.WriteLine("Does the specified string is in the StringCollection? = " + stringCol.Contains("800"));
        Console.WriteLine("Total number of elements = " + stringCol.Count);
        Console.WriteLine("Iterating through StringCollection = " + stringCol.Count);

        StringEnumerator myEnum = stringCol.GetEnumerator();
        while (myEnum.MoveNext())
        {
            Console.WriteLine(myEnum.Current);
        }
    }
}

ผลลัพธ์

Array elements...
100
200
300
400
500
Does the specified string is in the StringCollection? = False
Total number of elements = 5
Iterating through StringCollection = 5
100
200
300
400
500

ตัวอย่างที่ 2: เพิ่มข้อมูลทีละตัวและวนลูปด้วย StringEnumerator

using System;
using System.Collections.Specialized;

public class Demo
{
    public static void Main()
    {
        StringCollection stringCol = new StringCollection();
        stringCol.Add("10");
        stringCol.Add("20");
        stringCol.Add("30");
        stringCol.Add("40");
        stringCol.Add("50");
        stringCol.Add("60");
        stringCol.Add("70");
        stringCol.Add("80");
        stringCol.Add("90");
        stringCol.Add("100");

        Console.WriteLine("Elements...");
        foreach (string res in stringCol)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("Iterating through StringCollection with Enumerator...");
        StringEnumerator myEnum = stringCol.GetEnumerator();
        while (myEnum.MoveNext())
        {
            Console.WriteLine(myEnum.Current);
        }
    }
}

ผลลัพธ์

Elements...
10
20
30
40
50
60
70
80
90
100
Iterating through StringCollection with Enumerator...
10
20
30
40
50
60
70
80
90
100

สรุปและข้อแนะนำ

  • StringCollection อยู่ใน namespace System.Collections.Specialized — เป็นคอลเลกชันสำหรับสตริงแบบ non-generic (เก่า)
  • GetEnumerator() คืนค่า StringEnumerator ซึ่งมี MoveNext() และ Current
  • แนะนำให้ใช้ List<string> แทน ในโค้ดใหม่ — มี type safety, generic, เมธอดมากกว่า (Find, FindAll, Sort, LINQ)
  • StringCollection ยังคงมีอยู่เพื่อ backward compatibility
  • เมธอดที่น่าสนใจ: AddRange(), Contains(), IndexOf(), Insert(), Remove()

การใช้ List<string> แทน (แนะนำ)

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        var list = new List<string> { "10", "20", "30", "40", "50" };

        // foreach (แนะนำ)
        foreach (var s in list) Console.WriteLine(s);

        // Contains
        Console.WriteLine("Contains '30': " + list.Contains("30"));

        // LINQ
        var found = list.Find(x => x.StartsWith("2"));
        Console.WriteLine("Starts with 2: " + found);
    }
}