-1

Regarding the following code:

main()

List<int> a = new List<int>();
List<int> b = new List<int>();

a.Add(2);
a = add(a, 3);
b = add(a, 4);
b.Add(5);
a.Add(6);

function

static List<int> add(List<int> l, int x)
{
    l.Add(x);
    return l;
}

What I would like is that the result would be: a(2,3,6) and b(2,3,4,5).

In the end, both lists contain (2,3,4,5,6).

I understand that this may happens because a,b are just pointers to the start of the list. How could i achieve my desired result?

Thanos Darkadakis
  • 1,669
  • 2
  • 18
  • 30
  • 2
    see my answer here http://stackoverflow.com/questions/20877596/copying-list-values/20877640#20877640 – Ehsan Jan 08 '14 at 09:44
  • Why do you have the `add` method? It looks totally pointless – johnnycardy Jan 08 '14 at 09:45
  • Just return a different list from the one received as parameter. – Andrei V Jan 08 '14 at 09:50
  • @johnnycardy it seems pointless in this example. but it would make my question easier understandable. This is how i could use my function: a.add(1); for (i=2;i<10;i++){b=add(a,i); do_things_with_b();}. So in the first iteration i would run the function with b(1,2), in the second iteration with b(1,3)... until b(1,10) – Thanos Darkadakis Jan 08 '14 at 09:50
  • @ThanosDarkadakis - How about an extension method like `public static IEnumerable And(this IEnumerable source, T item) { foreach (var value in source) { yield return value; } yield return item; }` which you could then use like `var b = a.And(i);` or if you really need a list: `var b = a.And(i).ToList();`? Would even work for `var b = a.And(3); b = b.And(4);` (or `var b = a.And(3).And(4);` for that matter) - now `b` would enumerate everything from `a`, then `3` and then `4`. – Corak Jan 08 '14 at 10:55
  • @ThanosDarkadakis Have a look at my answer. I'm really curious to know if it's a valid solution - so much shorter and simpler. – johnnycardy Jan 08 '14 at 12:21

3 Answers3

7

Here is a complete example that does what you want:

class Program
{
    static void Main(string[] args)
    {
        List<int> a = new List<int>();
        List<int> b = new List<int>();

        a.Add(2);
        a = add(a, 3);
        b = add(a, 4);
        b.Add(5);
        a.Add(6);

        foreach (var item in a)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine();

        foreach (var item in b)
        {
            Console.WriteLine(item);
        }
    }

    static List<int> add(List<int> l, int x)
    {
        List<int> result = new List<int>(l);
        result.Add(x);
        return result;
    }
}
mrzli
  • 16,799
  • 4
  • 38
  • 45
  • Although it works, it seems overkill to create a new list every time an item is added. – johnnycardy Jan 08 '14 at 10:03
  • @johnnycardy It works, it is simple and it is the only thing I can think of at this time for this requirement. If creating a new list every time is an issue, I'm guessing there would not be a requirement to make something work this way. – mrzli Jan 08 '14 at 10:10
2

As you want two separate lists, you have to create another list in the code. If you want the method to return a new list, and get the result that you desired, you would need to create it before adding the item to it:

static List<int> add(List<int> l, int x) {
  l = new List<int>(l);
  l.Add(x);
  return l;
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Here's how I would do it. Delete your add method, and include System.Linq. Then you can use the ToList() method to create a copy.

List<int> a = new List<int>();
List<int> b; //Don't instantiate - it would be overwritten anyway

a.Add(2);
a.Add(3);

b = a.ToList(); //Create a copy of 'a'
b.Add(4);
b.Add(5);

a.Add(6);
johnnycardy
  • 3,049
  • 1
  • 17
  • 27