Resources » Code Snippets » C# Syntax

Difference between Call by value & Call by reference


Posted Date: 12-Dec-2008  Last Updated:   Category: C# Syntax    
Author: Member Level: Silver    Points: 7



Call by value :
using System;

namespace call_by_value
{
class c
{
public int x=100;
public void method(int x)
{
x=x+x;
Console.WriteLine("within the method : "+x.ToString());
}
public static void Main()
{
c obj=new c();
Console.WriteLine(obj.x);
obj.method(obj.x);
Console.WriteLine("after coming out of the method : "+obj.x);
Console.ReadLine();
}
}
}

Call by Reference

using System;
namespace call_by_reference
{
class c
{
public int x=100;
public void method(ref int x)
{
x=x+x;
Console.WriteLine("within the method : "+x.ToString());
}
public static void Main()
{
c obj=new c();
Console.WriteLine(obj.x);
obj.method(ref obj.x);
Console.WriteLine("after coming out of the method : "+obj.x);
Console.ReadLine();
}
}

}

We use 'ref' keyword to pass reference value.


Did you like this resource? Share it with your friends and show your love!

Responses to "Difference between Call by value & Call by reference"
Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Roman Goryelov
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India