-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Varad_RS
Message 2 in Discussion
Here you go.. When we pass a parameter as ref to a method, the method refers to the
same variable and changes made will affect the actual variable.Even the variable
passed as out parameter is similar to ref, but there are few implementation
differences when you use it in C# . Argument passed as ref must be initialized
before it is passed to the method, where as in case of out its is not necessary,but
after a call to the method as an out parameter the variable must be initialized.
When to use ref and out parameter. out parameter can be used when we want to return
more than one value from a method. NOTE : We now know what are ref and out
parameters, but these are only for C#(these are only understood by csc Compiler) when
looking inside the IL Code there is no difference whether you use ref or out
parameters. The implementation of ref and out parameter in IL Code is same. When
Calling a method and in the method signature after the datatype of the parameter a &
sign is used, indicating the address of the variable.
Code Sample: using System;
class CSRefOut
{
public static void Main(String [] args)
{
int a = 0,b,c=0,d=0;
Console.WriteLine("a is normal parameter will not affect the changes after
the function call");
Console.WriteLine("b is out parameter will affect the changes after the
function call but not necessary to initialize the variable b but should be initialized
in the function ParamTest ");
Console.WriteLine("c is ref parameter will affect the changes after the
function call and is compulsory to initialize the variable c before calling the
function ParamTest");
Console.WriteLine("d is used to store the return value");
d=ParamTest(a,out b,ref c);
Console.WriteLine("a = {0}", a);
Console.WriteLine("b = {0}", b);
Console.WriteLine("c = {0}", c);
Console.WriteLine("d = {0}", d);
}
public static int ParamTest(int a, out int b, ref int c)
{
a=10;
b=20;
c=30;
return 40;
}
}
Save the Above File as RefOut.Cs , Compile C:\>csc RefOut.cs and Run C:\>RefOut
Hope this helps..
Regards,
Varad.
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]