一千萬個為什麽

搜索

C# - 訪問從List <string>繼承的集合的項目

I am trying to implement a new class inherited from List, to load the contents from a text file to the items.

using System.Collections.Generic;
using System.IO;
using System.Linq;

public class ListExt : List
{
    string baseDirectory;

    public void LoadFromFile(string FileName)
    {
        //does not work because _list is private
        this._items = File.ReadAllLines(FileName).ToList();
    }
}

但我不知道如何將行加載到 _items 屬性中,因為它是私有的。

有什麽建議麽?

最佳答案

ListExt is the list - so you can just call AddRange on yourself. You can't reassign the list - that would essentially be changing this.

轉載註明原文: C# - 訪問從List <string>繼承的集合的項目