0% found this document useful (0 votes)
15 views2 pages

Ref and Out C Sharp

Uploaded by

Madhuri Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Ref and Out C Sharp

Uploaded by

Madhuri Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PASS BY VALUE AND PASS BY REFERENCE IN C#

(REF AND OUT KEYWORDS)

PASS BY VALUE
5

static void Main(string[] args) Static void PassByValue (int a)


{ 5 {
int value = 5; a = a + 10;
PassByValue(value); 15 console.writeline(“value is: ” + a);
console.writeline(value); }
}

PASS BY REFERENCE

Static void PassByRef (ref int a)


static void Main(string[] args) {
{ a = a + 10;
15

abc
console.writeline(“value is: ” + a);
int value = 5; }
PassByRef(ref value);15
console.writeline(value);
}
PASS BY OUT

static void Main(string[] args) Static void PassByOut (out int a)

{ {

int value; a = 20;

PassByOut(out value); console.writeline(“value is: ” + a);

console.writeline(value); }

PASS BY REFERENCE (REF KEYWORD)


 The ref keyword causes arguments to be passed in a method by reference.
 In call by reference, the called method changes the value of the parameters
passed to it from the calling method.
 Any changes made to the parameters in the called method will be reflected
in the parameters passed from the calling method when control passes
back to the calling method.
 It is necessary that both the called method and the calling method must
explicitly specify the ref keyword before the required parameters.
 The variables passed by reference from the calling method must be first
initialized.

OUT KEYWORD
 The out keyword is similar to the ref keyword and causes arguments to be
passed by reference.
 The only difference between the two is that the out keyword does not
require the variables that are passed by reference to be initialized.
 Both the called method and the calling method must explicitly use the out
keyword.

You might also like