문제

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.

도움이 되었습니까?

해결책

Here's an encapsulated method:

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
}

This may not be the cleanest code, but it does not inherit from List<T>.

다른 팁

To derive a class from List<string>, use the following syntax:

public class LString : List<string>
{
}

It is not possible to further derive the class from an array. So you'll have to be happy with:

LString[] myvar = new LString[5];

Edit:

Based on feedback, you're better off doing something like this to encompass your list:

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);
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top