Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Why the output is like this ?

Why the output is like this ?

Scheduled Pinned Locked Moved C#
questionhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kamrul Hasan
    wrote on last edited by
    #1

    Consider the following code:

    	public class A
    	{
    		private int x = 0;
    		public int X
    		{
    			get
    			{
    				return x;
    			}
    			set
    			{
    				x = value;
    			}
    		}
    		public override string ToString()
    		{
    			return x.ToString();
    		}
    	}
    
    	public class B
    	{
    		private ArrayList list = new ArrayList();
    
    		private void addItem( A c )
    		{
    			list.Add(c);
    		}
    		private void changeItem ( ArrayList l )
    		{
    			((A)l[0]).X = 5;
    		}
    		public void test() 
    		{
    			addItem( new A());
    			Console.WriteLine(list[0]);
    			changeItem(list);
    			Console.WriteLine(list[0]);
    		}
    		public static void Main( string[] args ) 
    		{
    			B b = new B();
    			b.test();
    		}
    	}
    

    The output is:

    0
    5
    

    Why the output not like:

    0
    0
    

    changeItem method does not take the reference of the ArrayList, but the value of the object in the collection has changed :omg: What is the problem ?

    J 1 Reply Last reply
    0
    • K Kamrul Hasan

      Consider the following code:

      	public class A
      	{
      		private int x = 0;
      		public int X
      		{
      			get
      			{
      				return x;
      			}
      			set
      			{
      				x = value;
      			}
      		}
      		public override string ToString()
      		{
      			return x.ToString();
      		}
      	}
      
      	public class B
      	{
      		private ArrayList list = new ArrayList();
      
      		private void addItem( A c )
      		{
      			list.Add(c);
      		}
      		private void changeItem ( ArrayList l )
      		{
      			((A)l[0]).X = 5;
      		}
      		public void test() 
      		{
      			addItem( new A());
      			Console.WriteLine(list[0]);
      			changeItem(list);
      			Console.WriteLine(list[0]);
      		}
      		public static void Main( string[] args ) 
      		{
      			B b = new B();
      			b.test();
      		}
      	}
      

      The output is:

      0
      5
      

      Why the output not like:

      0
      0
      

      changeItem method does not take the reference of the ArrayList, but the value of the object in the collection has changed :omg: What is the problem ?

      J Offline
      J Offline
      Joshua Quick
      wrote on last edited by
      #2

      Kamrul Hasan wrote:

      changeItem method does not take the reference of the ArrayList, but the value of the object in the collection has changed What is the problem ?

      Actually, changeItem() is taking the reference to your ArrayList member variable. Look again in your test() code. You're passing a reference to ArrayList member variable "list" into changeItem(). public void test() { addItem( new A()); Console.WriteLine(list[0]); changeItem(list); // You are passing it here! Console.WriteLine(list[0]); }

      K 1 Reply Last reply
      0
      • J Joshua Quick

        Kamrul Hasan wrote:

        changeItem method does not take the reference of the ArrayList, but the value of the object in the collection has changed What is the problem ?

        Actually, changeItem() is taking the reference to your ArrayList member variable. Look again in your test() code. You're passing a reference to ArrayList member variable "list" into changeItem(). public void test() { addItem( new A()); Console.WriteLine(list[0]); changeItem(list); // You are passing it here! Console.WriteLine(list[0]); }

        K Offline
        K Offline
        Kamrul Hasan
        wrote on last edited by
        #3

        Thanks for your reply. In which cases 'ref' keyword should be used? I am confused :confused: As your answer the following two methods will do the same thing isn't it? 1.changeItem( ref ArrayList l ) or 2.changeItem( ArrayList l )

        J 1 Reply Last reply
        0
        • K Kamrul Hasan

          Thanks for your reply. In which cases 'ref' keyword should be used? I am confused :confused: As your answer the following two methods will do the same thing isn't it? 1.changeItem( ref ArrayList l ) or 2.changeItem( ArrayList l )

          J Offline
          J Offline
          Joshua Quick
          wrote on last edited by
          #4

          Member variable "list" is a reference to the ArrayList object and not the object itself. The important thing you need to learn here is the difference between "reference types" and "value types". All classes such as ArrayList are reference types. Class objects are created via "new" and they only exist in the heap (not the stack). "new" merely returns a reference to that new object. So, when you pass an object reference to a function, you are merely passing a copy of that reference and not a copy of that object. All structures such as int, long, and float are value types. These types are created on the stack and are not referenced. And yes, they really are structures. int maps to the System.Int32 structure. float maps to the System.Single structure. So, when you pass a value type into a function, you are passing a copy of its data. I'm not sure what your goal is here, but if you want to pass a copy of your ArrayList object into your changeItem() function, then you're going to need to clone it. Cloning an object will create a new object having the same object data. changeItem((ArrayList)list.Clone()); I hope this helps!

          S 1 Reply Last reply
          0
          • J Joshua Quick

            Member variable "list" is a reference to the ArrayList object and not the object itself. The important thing you need to learn here is the difference between "reference types" and "value types". All classes such as ArrayList are reference types. Class objects are created via "new" and they only exist in the heap (not the stack). "new" merely returns a reference to that new object. So, when you pass an object reference to a function, you are merely passing a copy of that reference and not a copy of that object. All structures such as int, long, and float are value types. These types are created on the stack and are not referenced. And yes, they really are structures. int maps to the System.Int32 structure. float maps to the System.Single structure. So, when you pass a value type into a function, you are passing a copy of its data. I'm not sure what your goal is here, but if you want to pass a copy of your ArrayList object into your changeItem() function, then you're going to need to clone it. Cloning an object will create a new object having the same object data. changeItem((ArrayList)list.Clone()); I hope this helps!

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            Just in case the OP misses it, only the ArrayList is cloned, not the elements in that list. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            J 1 Reply Last reply
            0
            • S S Senthil Kumar

              Just in case the OP misses it, only the ArrayList is cloned, not the elements in that list. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              J Offline
              J Offline
              Joshua Quick
              wrote on last edited by
              #6

              Since the ArrayList contains integers (ie: value types), it should be copied ok. But good point to bring up. If it stored reference types, then they'll need to be cloned too.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups