C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

Diff b/w out & ref parameters?


Posted Date: 10 Oct 2008      Posted By: Mahima      Member Level: Silver     Points: 1   Responses: 3



Can any1 tel m d diff b/w out & ref parameters?




Responses

Author: senthil    10 Oct 2008Member Level: GoldRating: 2 out of 52 out of 5     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 2008Member Level: GoldRating: 2 out of 52 out of 5     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 2008Member Level: DiamondRating: 2 out of 52 out of 5     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



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : How integrate gears into dotnet webbrowser
Previous : Indexers in C#.NET?
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use