Home
|
Submit an interview question
|
Filter by category
|
Filter by job function
|
Filter by company
Member Information
User Id
Password
Remember Me
Forgot Password
Technology Category
.NET
(
1202
)
Languages
(
103
)
Database
(
146
)
Operating System
(
48
)
Reporting
(
5
)
Third-Party Tools
(
2
)
Testing
(
91
)
OOP
(
75
)
Web Development
(
40
)
Design Patterns
(
89
)
General
(
9
)
Networking
(
88
)
Hardware
(
63
)
Brain Exercise
(
2
)
Others
(
37
)
Our Network
.NET Heaven
C# Corner
Interview Corner
Longhorn Corner
Mindcracker
VB.NET Heaven
Home
»
Languages
» Languages
what is the difference between ref keyword and out keyword in C#?
Posted by
Oct 26, 2006
Viewed
:
8763
times
Major Category :
Languages
Minor Category :
C-Sharp
Total Replies :
4
Sponsored by
Become a Sponsor
EDITORIAL ANSWER
No Reply Yet
ANSWERS BY USERS
Nov 07, 2006
The ref keyword causes arguments to be passed by reference. The effect is that any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method. To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword
An argument passed to a ref parameter must first be initialized. This differs from out, whose argument need not be explicitly initialized before being passed
Although ref and out are treated differently at run-time, they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a ref argument and the other takes an out argument.
These two methods, for example, are identical in terms of compilation, so this code will not compile:
class CS0663_Example
{
// compiler error CS0663: "cannot define overloaded
// methods that differ only on ref and out"
public void SampleMethod(ref int i) { }
public void SampleMethod(out int i) { }
}
Overloading can be done, however, if one method takes a ref or out argument and the other uses neither, like this:
class RefOutOverloadExample
{
public void SampleMethod(int i) { }
public void SampleMethod(ref int i) { }
}
Oct 26, 2006
The ref keyword causes arguments to be passed by reference. The effect is that any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method. To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword
An argument passed to a ref parameter must first be initialized. This differs from out, whose argument need not be explicitly initialized before being passed
Although ref and out are treated differently at run-time, they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a ref argument and the other takes an out argument.
These two methods, for example, are identical in terms of compilation, so this code will not compile:
class CS0663_Example
{
// compiler error CS0663: "cannot define overloaded
// methods that differ only on ref and out"
public void SampleMethod(ref int i) { }
public void SampleMethod(out int i) { }
}
Overloading can be done, however, if one method takes a ref or out argument and the other uses neither, like this:
class RefOutOverloadExample
{
public void SampleMethod(int i) { }
public void SampleMethod(ref int i) { }
}
Nipun Tomar
Oct 26, 2006
Refer this url:
http://www.c-sharpcorner.com/Language/out_and_ref.asp
Dinesh Beniwal
Oct 26, 2006
Difference is that out parameters need to be declared but not initialized.
Ref parameter will have a value even before the method call..
Anyhow, any change to this argument in the called method will affect the value of the actual variable.
1
Hosted by MaximumASP
|
Found a broken link?
|
Contact Us
|
Terms & conditions
|
Privacy Policy
|
Advertise with us
Current Version:
2.2009.3.2
© 1999 -
2009
Mindcracker LLC. All Rights Reserved
Channels
:
Jobs
|
Interviews
|
Consulting
|
Training
|
Photos
|
Authors
|
Tips
|
Forums
|
E-Books
|
Blogs
Programming
:
C#
|
Visual Basic
|
ASP.NET & Web Development
|
C++
|
Other .NET Languages
|
Windows Vista
|
XAML
|
Tutorials
Sponsors
:
ASP.NET Hosting
|
Clickatell
|
Dundas Software
|
DynamicPDF
|
RedGate Software
|
Visual Studio 2010