| Author: senthil 10 Oct 2008 | Member Level: Gold | Rating:  Points: 4 |
ref use this code:
public class pass { public void pass1(int c) { c*=c; Console.WriteLine("Processing the pass value :"+c); } public void refer(ref int b) { b=b+10; Console.WriteLine("Processing the Refer value :"+b); } }
|
| Author: gomathinayagam 10 Oct 2008 | Member Level: Gold | Rating:  Points: 6 |
Ref and out parameter passing modes are used to allow a method to alter variables passed in by the caller. The difference between ref and out is subtle but important. Each parameter passing mode is designed to apply to a slightly different programming scenario. The important difference between out and ref parameters is the definite assignment rules used by each.
The caller of a method which takes an out parameter is not required to assign to the variable passed as the out parameter prior to the call; however, the callee is required to assign to the out parameter before returning.
Here's a simple example:
class OutExample { // Splits a string containing a first and last name separated // by a space into two distinct strings, one containing the first name and one containing the last name
static void SplitName(string fullName, out string firstName, out string lastName) { // NOTE: firstName and lastName have not been assigned to yet. Their values cannot be used. int spaceIndex = fullName.IndexOf(' '); firstName = fullName.Substring(0, spaceIndex).Trim(); lastName = fullName.Substring(spaceIndex).Trim(); }
static void Main() { string fullName = "Yuan Sha"; string firstName; string lastName;
// NOTE: firstName and lastName have not been assigned yet. Their values may not be used. SplitName(fullName, out firstName, out lastName); // NOTE: firstName and lastName have been assigned, because the out parameter passing mode guarantees it.
System.Console.WriteLine("First Name '{0}'. Last Name '{1}'", firstName, lastName); } }
Gomathi Nayagam
Please Rate my answer if it has helpful to you
|
| Author: www.DotNetVJ.com 10 Oct 2008 | Member Level: Diamond | Rating:  Points: 5 |
Hi
A variable passed as an out argument need not be initialized. However, the out parameter must be assigned a value before the method returns.
An argument passed to a ref parameter must first be initialized. Compare this to an 'out ' parameter, whose argument does not have to be explicitly initialized before being passed to an out parameter.
Check out the below link http://narencoolgeek.blogspot.com/2005/06/whats-difference-between-out-and-ref.html
Thanks -- Vj http://dotnetvj.blogspot.com http://oravj.blogspot.com
Thanks -- Vijaya Kadiyala http://www.DotNetVJ.com Microsoft MVP Me & My Little Techie
|