Passing by value and passing by reference

by Jagadish Pulakhandam on 9/8/2011 12:33:19 PM
Rated 0 from 0 votes
Brief: Demonstrates on calling a method by value vs. method by reference (using both value types and reference types)
Posted to: Basics of Object Oriented Programming in .NET
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Demonstrates the following:
  • Pass parameters by value to a method
  • Pass parameters by reference to a method ("ref" keyword)
  • Pass objects by reference to a method (using "ref" and without using "ref")
Follow comments inline.

Source Code:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.namespace PassByReference
07.{
08.    public class Employee
09.    {
10.        public string Empno;
11.        public string Ename;
12.        public double Sal;
13.    }
14.}
001.using System;
002.using System.Collections.Generic;
003.using System.ComponentModel;
004.using System.Data;
005.using System.Drawing;
006.using System.Linq;
007.using System.Text;
008.using System.Windows.Forms;
009. 
010.namespace PassByReference
011.{
012.    public partial class Form1 : Form
013.    {
014.        public Form1()
015.        {
016.            InitializeComponent();
017.        }
018. 
019.        private void button1_Click(object sender, EventArgs e)
020.        {
021.            //-----------------------------------
022.            //Working with Value Types (not objects/instances)
023.            //------------------------------------------------
024. 
025.            //Call a method by value.
026.            int i = 10; //a primitive type
027.            MessageBox.Show("In button1_Click. value of i: " + i.ToString()); //displays 10
028.            //A memory clone of "i" is sent to method (new memory location gets created, value of "i" is copied into that and sent to the method).
029.            Increment(i); //displays 11
030.            //"i" will not be affected, even if method "Increment" modified the value (as we did not send the original memory location of "i")
031.            MessageBox.Show("In button1_Click. value of i: " + i.ToString()); //displays 10
032. 
033. 
034.            //Call a method by reference.
035.            int j = 20;
036.            MessageBox.Show("In button1_Click. value of j: " + j.ToString()); //displays 20
037.            //No clone is made (no new memory).  The memory location of "j" is sent (which implies value as well)
038.            Decrement(ref j); //displays 21
039.            //"j" will be affected, if method "Decrement" modified the value (as we send the memory of "j" directly)
040.            MessageBox.Show("In button1_Click. value of j: " + j.ToString()); //displays 21
041. 
042.            //------------------------
043.            //Working with Objects
044.            //-----------------------
045.            Employee o = new Employee()
046.            {
047.                Empno = "1001",
048.                Ename = "Jag",
049.                Sal = 3400
050.            };
051.            //Objects are passed by reference by default (no clone)
052.            IncreaseSal(o); //displays 4400
053.            //o.Sal gets affected, if method "IncreaseSal" modified the field (as we send the memory of "o" directly, by default)
054.            MessageBox.Show("In button1_Click method. value of Sal: " + o.Sal.ToString()); //displays 4400
055. 
056. 
057.            //by now, o.Sal is 4400
058.            //Objects are passed by reference by default (no clone)
059.            ModifyEverything(o); //displays 5400
060.            //o.Sal does not get affected as the method "ModifyEverything" created a new memory area for "p" (local to itself)
061.            MessageBox.Show("In button1_Click method. value of Sal: " + o.Sal.ToString()); //displays 4400
062. 
063. 
064.            //by now, o.Sal is 4400
065.            //Objects are passed with "strict" reference by default (no clone)
066.            ModifyEverythingStrictly(ref o); //displays 5400
067.            //"o" gets affected in all the ways possible, if "ModifyEverythingStrictly" modifies "p" in any way
068.            MessageBox.Show("In button1_Click method. value of Sal: " + o.Sal.ToString()); //displays 5400
069. 
070.        }
071. 
072.        public void Increment(int a) //"a" will always its own memory area
073.        {
074.            a++; //this will have no affect on the variable passed, only "a" gets changed.
075.            MessageBox.Show("In Increment method. value of a: " + a.ToString());
076.        }
077. 
078.        public void Decrement(ref int b) //"b" will never have its own memory.  It is just refers another memory area.
079.        {
080.            b++;
081.            MessageBox.Show("In Decrement method. value of b: " + b.ToString());
082.        }
083. 
084.        public void IncreaseSal(Employee p)
085.        {
086.            p.Sal += 1000;
087.            MessageBox.Show("In IncreaseSal method. value of Sal: " + p.Sal.ToString());
088.        }
089. 
090.        public void ModifyEverything(Employee p)
091.        {
092.            p = new Employee()
093.            {
094.                Empno = "1002",
095.                Ename = "Winner",
096.                Sal = 5400
097.            };
098.            MessageBox.Show("In ModifyEverything method. value of Sal: " + p.Sal.ToString());
099.        }
100. 
101.        public void ModifyEverythingStrictly(ref Employee p)
102.        {
103.            p = new Employee()
104.            {
105.                Empno = "1002",
106.                Ename = "Winner",
107.                Sal = 5400
108.            };
109.            MessageBox.Show("In ModifyEverythingStrictly method. value of Sal: " + p.Sal.ToString());
110.        }
111.    }
112.}


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following