C # - access to elements of a collection inherited from List <string>

I am trying to implement a new class inherited from List<string>to load contents from a text file into elements.

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

public class ListExt : List<string>
{
    string baseDirectory;

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

But I do not know how to load strings into a property _items, because it is personal.

Any suggestions?

+3
source share
4 answers

ListExt- This is a list, so you can just call AddRangefor yourself. You cannot reassign the list - that would make a big difference this.

+7
source

Try something like:

public void LoadFromFile(string FileName)
{
    base.AddRange(File.ReadAllLines(FileName));
}
+6
source

List<T> .

List<string> lines = new List<string>(File.ReadAllLines(path));
+1

:

AddRange(File.ReadAllLines(FileName).ToList());
0

Source: https://habr.com/ru/post/1741308/


All Articles