Turbosheep
Turbosheep

Reputation: 183

Add from one list to another

I have been trying to 'transport' the first two elements from list A to list B without copying them.

At the start, list A has four int values. In the end I want List A and B to both have 2 int values.

I'm trying something like this:

int a = 1;
int b = 2;
int c = 3;
int d = 4;

TestA.Add(a);
TestA.Add(b);
TestA.Add(c);
TestA.Add(d);

for (int i=0; i<2; i++)
{
    TestB.AddRange(TestA[i]);
}

I get a IEnummerable conversion error. I'm sure I'm doing it in a very naive way, and I'd appreciate some help here. Thanks.

Upvotes: 0

Views: 1674

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

What's wrong with simple Take/Skip combination?

TestB = TestA.Take(2).ToList();
TestA = TestA.Skip(2).ToList();

Enumerable.Take - takes N elements from beginning of the collection.

Enumerable.Skip - skips N elements from beginning of the collection and takes all others.

Upvotes: 3

Related Questions