Passing reference type parameters to a method in C#

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

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 objectConsole.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 valuestatic void PassPersonByVal(Person person){//change the properties of person objectperson.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 refstatic void PassPersonByRef(ref Person person){//change the properties of person objectperson.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.