By 22/07/2015 4 Comments

C# Pass By Value VS. Pass By Reference

  • C# pass by reference variables assign only reference to other reference variables and there are no any copy made of them.
  • Reference variables refer to the same object.
  • C# Pass by values variables are local variables which assigns its copy to other variables.


Example – 1, pass by reference:

using System;
using System;
namespace csharpBasic
{
    class MarkSheet
    {

   // String type field declaration.
        public string TechnologyTitle;

   // A void type method declaration Display() which passes a reference type.
        public void Display(MarkSheet markSheetTwo)
        {
            markSheetTwo.TechnologyTitle = "Asp.net";
        }
    }

    class Program
    {

    // Static main method void type declaration.
        static void Main(string[] args)
        {
            MarkSheet markSheetOne = new MarkSheet();

   // Field is initialized.
            markSheetOne.TechnologyTitle = "C#";
            Console.WriteLine("Before pass by reference method call, technology is: {0}", markSheetOne.TechnologyTitle);

   // Display() method is being called through an object reference variable.
            markSheetOne.Display(markSheetOne);
            Console.WriteLine("After pass by reference method call, technology is: {0}", markSheetOne.TechnologyTitle);
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Before pass by reference method call, technology is: C#
        After pass by reference method call, technology is: Asp.net
          
       */
    }
}


Example – 2, pass by value:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
     // A void type method is declared with a value type parameter (technologyTitle).
        public void Display(string technologyTitle)
        {
            technologyTitle = "Php";
            Console.WriteLine("This is a copy of technologyTitle variable: {0}", technologyTitle);
        }
    }

    class Program
    {

    // Static main method void type declaration.
        static void Main(string[] args)
        {
            MarkSheet markSheetOne = new MarkSheet();

    // Initialize a local variable technologyTitle with string value.
            string technologyTitle = "C#";
            Console.WriteLine("Before pass by value method call, technology is: {0}", technologyTitle);

   // Display() method is being called through an object reference.
            markSheetOne.Display(technologyTitle);
            Console.WriteLine("After pass by value method call no changes made, technology is: {0}", technologyTitle);
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Before pass by value method call, technology is: C#
        This is a copy of technologyTitle variable: Php
        After pass by value method call no changes made, technology is: C#
          
       */
    }
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .