0
        static void asksomethinglater(IntNode b)//
        { 
            IntNode temp = b;
            b = b.GetNext();
            temp.SetNext(null);

        }

I have this code that should upadte the argument b, but when ever i run this, i have this object as an example IntNode b = new IntNode(3, new IntNode(5, new IntNode(2)));

The object will have only 5, with this.next = null

the class i use.

    public class IntNode
    {
        private int info;
        private IntNode next;

        public IntNode(int info)
        {
            this.info = info;
            this.next = null;
        }

        public IntNode(int info, IntNode next)
        {
            this.info = info;
            this.next = next;
        }

        public int GetInfo()
        {
            return info;
        }

        public IntNode GetNext()
        {
            return next;
        }

        public void SetInfo(int info)
        {
            this.info = info;
        }

        public void SetNext(IntNode next)
        {
            this.next = next;
        }

        public override string ToString()
        {
            if(next != null)
                return info + "-->" + next;
            return info.ToString();
        }
    }
}
Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35

1 Answers1

0

In this function

static void asksomethinglater(IntNode b)

the variable b is passed by value. So the original reference was not changed.

So in this statement

b = b.GetNext();

there is being changed a copy of the original reference.

If you want to change the reference itself then you could declare the function like

static void asksomethinglater( ref IntNode b)
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335