By 22/07/2015 10 Comments

Pass By Value To Method In C#

  • When a value type such as (int, double, bool etc.) is being passed to a method as a parameter it is called pass by value to method in C#.
  • When a value is passed to any method as an argument, that the copy of that value is made.


Example:

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






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 .