By 22/07/2015 5 Comments

Pass By Reference To Method In C#

  • Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.
  • In pass by reference, there are no any copy made of a reference type variable.


Example:

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
          
       */
    }
}






← 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 .