Reference Method

A reference parameter is used for both input and output parameter passing. The argument passed for a reference parameter must be a variable, and during execution of the method, the reference parameter represents the same storage location as the argument variable. A reference parameter is declared with the ref modifier. The following example shows the use of ref parameters.


using System;
class Test
{
static void Swap(ref int x, ref int y) {
int temp = x;
x = y;
y = temp;
}
static void Main() {
int i = 1, j = 2;
Swap(ref i, ref j);
Console.WriteLine("{0} {1}", i, j); // Outputs "2 1"
}
}



Reference: www.dotnetmaterial9.blogspot.com


Comments

Author: Sanjay21 Jul 2010 Member Level: Silver   Points : 2

namespace ref_keyword
{
class keyword
{
static void Main(string[] args)
{
int num = 8;
calcsqure(num);
Console.WriteLine("Value of num without using ref keyword:{0}",num);

refsqure(ref num);
Console.WriteLine("Value of num using ref keyword:{0}",num);
}

static void calcsqure(int inum)
{
inum = inum * inum;
}

static void refsqure(ref int inum)
{
inum = inum * inum;
}
}
}



  • 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:
    Email: