开发者

How can I add a specified number of list elements to an ILIST?

开发者 https://www.devze.com 2023-03-12 21:42 出处:网络
I have a class as follows: public class ABC { public IList<TextFillerDetail> TextFillerDetails { get { return _textfillerDetails; } }

I have a class as follows:

public class ABC {
 public IList<TextFillerDetail> TextFillerDetails        
 { get { return _textfillerDetails; } }        
private List<TextFiller> _textfillerDetails = new List<TextFiller>();
}

I instantiate this class and add some TextDetails to it:

var ans = new ABC();
ans.TextDetails.Add(new TextDetail());
ans.TextDetails.Add(new TextDetail());
ans.TextDetails.Add(new TextDetail());
ans.TextDetails.Add(new TextDetail());

Is there a way that I could do this in one step by adding some code to the class such as a different kind of constructor. For example by passing in a number 5 to request that five elements be added?

开发者_JS百科var ans = new ABC(5);


You could add it as a constructor argument:

public class ABC()
{
    public ABC(int count)
    {
        for (int i = 0; i < count; i++) 
        {
            TextDetails.Add(new TextDetail());
        }
    }

    // Stuff
}


Sure, you could use a constructor that will initialize the list:

public class ABC 
{
    public ABC(int count)
    {
       if (count < 1) 
       {
           throw new ArgumentException("count must be a positive number", "count");
       }
        _textfillerDetails = Enumerable
            .Range(1, count)
            .Select(x => new TextDetail())
            .ToList();
    }

    public IList<TextFillerDetail> TextFillerDetails { get { return _textfillerDetails; } }        
    private List<TextFiller> _textfillerDetails;
}


Sure:

public class ABC {
 public IList<TextFillerDetail> TextFillerDetails        
 { get { return _textfillerDetails; } }        
  public ABC(int capacity)
  {
    _textfillerDetails = new List<TextFiller>(capacity);
  }
private List<TextFiller> _textfillerDetails;
}


There are a few ways:

Use an initializer; it saves a little typing:

var ans = new ABC{
    new TextDetail(),
    new TextDetail(),
    new TextDetail(),
    new TextDetail(),
    new TextDetail(),
}

Better idea: Use Linq to repeat an initialization lambda:

var ans = Enumerable.Repeat(0,5).Select(x=>new TextDetail()).ToList();


You could put in an overloaded constructor that takes the number of items to add as a parameter.

But why do you need to do that? Couldn't you just add TextDetail objects to your list as necessary?


Just for this task , Yes,

private List<TextFiller> _textfillerDetails = new List<TextFiller>();
public ABC(int capacity)
  {
     for(int index  = 0; index < capacity; index ++)
       _textfillerDetails.Add(new TextDetail());
  }


You can use a for loop or linq:

public class ABC
{
    public IList<TextFillerDetail> TextFillerDetails { get; private set }

    public ABC() : this(0)
    {
    }

    public ABC(int count)
    {
        TextFIllerDetails = Enumerable.Range(0,count)
                                      .Select(x => new TextFillerDetail())
                                      .ToList();
    }
}


Consider using also,

IEnumerable or ICollection or IQueryable objects.

Ray Akkanson

0

精彩评论

暂无评论...
验证码 换一张
取 消