Emily
Emily

Reputation: 237

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

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?

var ans = new ABC(5);

Upvotes: 2

Views: 90

Answers (8)

Ray Akkanson
Ray Akkanson

Reputation: 1

Consider using also,

IEnumerable or ICollection or IQueryable objects.

Ray Akkanson

Upvotes: 0

Zebi
Zebi

Reputation: 8882

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();
    }
}

Upvotes: 0

Sergii Zagriichuk
Sergii Zagriichuk

Reputation: 5399

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());
  }

Upvotes: 0

Daniel Mann
Daniel Mann

Reputation: 59020

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?

Upvotes: 0

KeithS
KeithS

Reputation: 71563

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();

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

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;
}

Upvotes: 2

Matthew Abbott
Matthew Abbott

Reputation: 61589

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
}

Upvotes: 3

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

Sure:

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

Upvotes: 1

Related Questions