0

I figure I'm just using the wrong description of what I want and that's why I can't find an answer, but essentially I want to do the following:

// Go from this
List<string>[] myvar = new List<string>()[5];
myvar[4].Add("meow");

// To this
LString myvar = new LString();
myvar.Add("meow");

I initially tried doing a class public class LString : List<string>()[], but that isn't really valid syntax, so I didn't really know where to go from there.

4

2 回答 2

2

要从 派生类List<string>,请使用以下语法:

public class LString : List<string>
{
}

无法从数组进一步派生类。因此,您必须对以下内容感到满意:

LString[] myvar = new LString[5];

编辑:

根据反馈,您最好执行以下操作来包含您的列表:

public class LString
{
  private List<string>[] _lists = new List<string>[5];

  public void Add(int index, string value)
  {
    if (index < 0 || index > 4)
      throw new ArgumentOutOfRangeException("index");
    _lists[index].Add(value);
  }
}
于 2013-06-07T18:25:29.927 回答
2

这是一个封装的方法:

public class LString
{
    List<string>[] _strListArray;

    public LString(int size)
    {
        _strListArray = new List<string>[size];
    }

    public void Add(int index, string str)
    {
        _strListArray[index].Add(str);
    }

    public void Remove(int index, string str)
    {
        _strListArray[index].Remove(str);
    }

    // insert more code for list manipulation
}

这可能不是最干净的代码,但它不继承自List<T>.

于 2013-06-07T18:36:23.380 回答