0

I want to build a sublist with references to the elements of anoter list (i need few of them, that are meeting the conditions).

How am i trying to do that:

        List<int> mainList = new List<int>();
        List<int> subList = new List<int>();
        mainList.Add(5);
        subList.Add(mainList[0]); //case 1
        //subList[0] = mainList[0]; //case 2
        Console.WriteLine("{0}", mainList[0]);
        Console.WriteLine("{0}", subList[0]);
        subList[0]++;
        Console.WriteLine("{0}", mainList[0]);
        Console.WriteLine("{0}", subList[0]);
        mainList[0]+=2;
        Console.WriteLine("{0}", mainList[0]);
        Console.WriteLine("{0}", subList[0]);

In the first case i'm getting the copy instead of reference because the console output is:

5
5
5
6
7
6

In the second case i'm getting ArgumentOutOfRangeException because subList[0] is not initialized. So, how can i do this?

Also, according to speed/memory usage may be it's even better to keep List<int> of inedexes of the mainList instead of references?

CrazyWu
  • 647
  • 2
  • 8
  • 19
  • 1
    Why are you worried about the performance of code that does not work correctly? – Aluan Haddad Feb 02 '18 at 10:22
  • @AluanHaddad because there may be few correct solutions in my case, so i'm going to check which is better as well. – CrazyWu Feb 02 '18 at 10:24
  • This would work if you wrap your int in a class. Currently you are copying your int on line 4 – ManIkWeet Feb 02 '18 at 10:25
  • 1
    Well, I would strongly advise that you not optimize anything until you get it to behave correctly. Doing so leads to madness. – Aluan Haddad Feb 02 '18 at 10:26
  • @AluanHaddad please, don't pay that much attention to the last paragraph of the question, it's not exactly about the main problem, it's just an additional thoughts. – CrazyWu Feb 02 '18 at 10:29
  • @slawekwin in my case, list items wouldn't be swapped or deleted, so it's pretty safe. but 1st of all i want to know if it's possible to build such reference – CrazyWu Feb 02 '18 at 10:31
  • @ManIkWeet can you please explane a bit more about "wrap you int" in this case – CrazyWu Feb 02 '18 at 10:32
  • You can't build a reference to an int like that. Int is not an object type but a primary type. – Florian Feb 02 '18 at 10:32
  • @Florian sounds like a 1st good explanation, thank you – CrazyWu Feb 02 '18 at 10:33
  • I've found this that may be helping you : https://stackoverflow.com/questions/2985646/how-to-store-a-reference-to-an-integer-in-c – Florian Feb 02 '18 at 10:34

2 Answers2

2

Thanks to @ManIkWeet and @Florian, i've found the solution:

        public class TestClass{
           public int value;
        }

        ...

        List<TestClass> mainList = new List<TestClass>();
        List<TestClass> subList = new List<TestClass>();
        mainList.Add(new TestClass { value = 5 });
        subList.Add(mainList[0]);
        Console.WriteLine("{0}", mainList[0].value);
        Console.WriteLine("{0}", subList[0].value);
        subList[0].value++;
        Console.WriteLine("{0}", mainList[0].value);
        Console.WriteLine("{0}", subList[0].value);
        mainList[0].value+=2;
        Console.WriteLine("{0}", mainList[0].value);
        Console.WriteLine("{0}", subList[0].value);

Output is:

5
5
6
6
8
8
CrazyWu
  • 647
  • 2
  • 8
  • 19
0

Case 2: You can not assign the value in list when list is not containing any element because there is no memory allocalted to your list items in sublist.

you are getting the System.ArgumentOutOfRangeException because your list index is less than 0 or index is equal to or greater than list count

You can assign value via indexing when you have item in your list

Hope this help !

Pankaj
  • 1