Passing reference type parameters to a method in C#

Vishvvas
Posted by in C# category on for Intermediate level | Points: 250 | Views : 7794 red flag
Rating: 4 out of 5  
 1 vote(s)

To infer about how the reference types are passed to a method and what is the outcome.

Introduction

In developer fraternity, passing of reference type as a parameter to a method is always a topic of hot discussion. The two issues namely (i) passing the parameters (ii) type of parameters are always taken for one another and mostly it is concluded that the passing mechanism doesn’t make any difference to reference type parameters. It seems true to most of developers but actually it’s not complete truth. Let’s see how it works. We all know that by default all parameters are passed by value.

Example:

Examples are in C#.

We have a class named Person (it’s a reference type) as following.

public class Person

{

public string FirstName { get; set; }

public string LastName { get; set; }

public Person(string firstName, string lastName)

{

this.FirstName = firstName;

this.LastName = lastName;

}

}

Console application:

class Program

{

static void Main(string[] args)

{

Person myPerson = new Person("Anand","Rathod"); //instantiate the object

Console.WriteLine(myPerson.FirstName + " " + myPerson.LastName);

PassPersonByVal(myPerson);

Console.WriteLine("After pass by value" + myPerson.FirstName + " " + myPerson.LastName);

PassPersonByRef(ref myPerson);

Console.WriteLine("After pass by ref" + myPerson.FirstName + " " + myPerson.LastName);

Console.ReadLine();

}

 

//method where object is passed by value

static void PassPersonByVal(Person person)

{

//change the properties of person object

person.FirstName = "FirstNameByVal";

person.LastName = "LastNameByVal";

Console.WriteLine(person.FirstName + " " + person.LastName);

person = new Person("NewFirstNameByVal", "NewLastNameByVal");

Console.WriteLine(person.FirstName + " " + person.LastName);

}

 

//method where object is passed by ref

static void PassPersonByRef(ref Person person)

{

//change the properties of person object

person.FirstName = "FirstNameByRef";

person.LastName = "LastNameByRef";

Console.WriteLine(person.FirstName + " " + person.LastName);

person = new Person("NewFirstNameByRef", "NewLastNameByRef");

Console.WriteLine(person.FirstName + " " + person.LastName);

}

}

When a reference type is passed by value to a method then as the parameter is reference to Person object when the passed object is changed the original variable doesn’t get changed. (Refer to above code)

When a reference type is passed by reference and its values are changed then the original value is also changed. (Refer to above code)

Output:

Before calling the methods, FirstName:Anand  LastName:Rathod
In PassPersonByVal() FirstName:FirstNameByVal LastName:LastNameByVal
In PassPersonByVal() FirstName:NewFirstNameByVal LastName:NewLastNameByVal
After calling pass by valueFirstNameByVal  LastName:LastNameByVal
In PassPersonByRef() FirstName:FirstNameByRef  LastName:LastNameByRef
In  PassPersonByRef() FirstName:NewFirstNameByRef LastName:NewLastNameByRef
After calling pass by refFirstName:NewFirstNameByRef  LastName:NewLastNameByRef

Conclusion:

When parameters are passed by ref and such parameters are changed in the method then such changes are applied to original variable passed and such changes persist.

References:
http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx

Page copy protected against web site content infringement by Copyscape

About the Author

Vishvvas
Full Name: Vishwas Sutar
Member Level: HonoraryPlatinum
Member Status: Member,MVP
Member Since: 5/30/2011 2:13:10 AM
Country: India

http://www.dotnetfunda.com
Extensive and rich experience across gamut of technologies and programming languages like PB,VB,C++,VB.NET, C#, Classic ASP,ASP.NET, ASP.NET MVC.

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 7/26/2011 | Points: 25
Nice article Sir, basic concepts brush-up...

Thanks and Regards
Akiii

Login to post response

Comment using Facebook(Author doesn't get notification)