ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Microsoft Related  >>  C Sharp
 
 


 

 
 Visual Basic interview questions  Visual Basic Interview Questions
 C Sharp interview questions  C Sharp Interview Questions
 ASP.NET interview questions  ASP.NET Interview Questions
 VB.NET interview questions  VB.NET Interview Questions
 COM+ interview questions  COM+ Interview Questions
 ADO.NET interview questions  ADO.NET Interview Questions
 IIS interview questions  IIS Interview Questions
 MTS interview questions  MTS Interview Questions
 Crystal Reports interview questions  Crystal Reports Interview Questions
 BizTalk interview questions  BizTalk Interview Questions
 Dot Net interview questions  Dot Net Interview Questions
 Exchange Server interview questions  Exchange Server Interview Questions
 SharePoint interview questions  SharePoint Interview Questions
 Microsoft Related AllOther interview questions  Microsoft Related AllOther Interview Questions
Question
what is reference parameter?
what is out parameters?
what is difference these two?
 Question Submitted By :: Somavenki
I also faced this Question!!     Rank Answer Posted By  
 
  Re: what is reference parameter? what is out parameters? what is difference these two?
Answer
# 1
a ref parameter must first be initialized before being 
passed from the calling function to the called function.


but a out parameter need not be initialized, we can pass it 
directly
 
Is This Answer Correct ?    12 Yes 2 No
Anna
 
  Re: what is reference parameter? what is out parameters? what is difference these two?
Answer
# 2
normally while we are passing values to other methods it 
will pass copy of the values.But we use ref or out 
parameter it pass reference of values.
 
Is This Answer Correct ?    6 Yes 1 No
Aravazhi
 
 
 
  Re: what is reference parameter? what is out parameters? what is difference these two?
Answer
# 3
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 same as ref parameter, but implementation in 
c# is different, 
Arguement passed as ref parameter must be initialized 
before it is passed to the method. But in case of out 
parameter it is not necessary. But after a call to a method 
as out parameter it is necessary to initialize.
When to use out and ref parameter, out parameter is used 
when we want to return more than one value from a method.
 
Is This Answer Correct ?    6 Yes 1 No
Lakshmi
 
  Re: what is reference parameter? what is out parameters? what is difference these two?
Answer
# 4
Ref parameter can be used as both input and o/p parameter
out parameter can be used as only output parameter
 
Is This Answer Correct ?    9 Yes 1 No
Rvs Varma
 
  Re: what is reference parameter? what is out parameters? what is difference these two?
Answer
# 5
The main difference between them is ref parameter act as 
input/output  whereas out parameter is simply act as output.

Let us understand with an example:

Out parameters "out" parameters are output only parameters 
meaning they can only passback a value from a function.We 
create a "out" parameter by preceding the parameter data 
type with the out modifier. When ever a "out" parameter is 
passed only an unassigned reference is passed to the 
function. 

using System;
class Test
{
 static void out_test(out int x)
 {
  x=100;
 }
 static void Main()
 {
  int Myvalue=5;
  MyMethod(Myvalue);
  Console.WriteLine(out Myvalue);             
 }
}

Output of the above program would be 100 since the value of 
the "out" parameter is passed back to the calling part. 
Note 
The modifier "out" should precede the parameter being 
passed even in the calling part. "out" parameters cannot be 
used within the function before assigning a value to it. A 
value should be assigned to the "out" parameter before the 
method returns. 

Ref parameters "ref" parameters are input/output parameters 
meaning they can be used for passing a value to a function 
as well as to get back a value from a function.We create 
a "ref" parameter by preceding the parameter data type with 
the ref modifier. When ever a "ref" parameter is passed a 
reference is passed to the function. 

using System;
class Test
{
 static void Mymethod(ref int x)
 {
  x=x+ 100;
 }
 static void Main()
 {
  int Myvalue=5;
  MyMethod(Myvalue);
  Console.WriteLine(ref Myvalue);              
 }
}

Output of the above program would be 105 since the "ref" 
parameter acts as both input and output. 
Note 

The modifier "ref" should precede the parameter being 
passed even in the calling part. "ref" parameters should be 
assigned a value before using it to call a method.
 
Is This Answer Correct ?    11 Yes 3 No
Piyush Sharma
 
  Re: what is reference parameter? what is out parameters? what is difference these two?
Answer
# 6
out -doesnot keep initial value or assigned value.
ref-does
void f1(out int t){
t+=100 //give error becs t has no value  --true for ref
t=100 //possible
}
 
Is This Answer Correct ?    3 Yes 4 No
Vijaysaxena
 
  Re: what is reference parameter? what is out parameters? what is difference these two?
Answer
# 7
A reference parameter contain only reference of variable 
instead of value. In reference type parameter if changes 
occurs in variable than it affect the original value. But 
in out parameter it send only a copy of variable instead of 
reference therefore if u change in variable no changes 
occur in original value.
For example:- 
    class Program
    {       
        public void refparameter(ref int a)
        {           
            a += 5;
        }
        public void outparameter(out int p)
        {          
            p = 20;
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            int b,c;
            Console.Write("Enter a Number:");
            b = Int32.Parse(Console.ReadLine());
            p.refparameter(ref b);
//After the calling of refparameter it change the value of b
            Console.WriteLine("After calling refparameter 
method value of B is:{0}",b);            
            p.outparameter(out c);
            Console.WriteLine("After calling the 
outparameter method value of C is:{0}",c);            
            Console.ReadLine();
        }
    }
 
Is This Answer Correct ?    2 Yes 1 No
Dharmendra Nonia
 
  Re: what is reference parameter? what is out parameters? what is difference these two?
Answer
# 8
out is used only to get output from a function.
it means that we cannot give any input using an out 
parameter. Whatever value we assign to an out parameter 
before calling to a function will be worthless, we need to 
assign a value in the called function to our out parameter
before its first usage and if no usage we need to assign a 
value before control leave the function.(out can be passed 
to function while uninitialized)

ref parameter is used for both input and output. The only 
thing is we need to assign a value before its first usage. 
ref parameter must be initialized before passing to a 
function.
 
Is This Answer Correct ?    2 Yes 0 No
Sarun P.t
 
  Re: what is reference parameter? what is out parameters? what is difference these two?
Answer
# 9
In short ,

ref parameters needs to be initialized and can be used as 
input as well as for output where as the out parameters 
needs not to be initialized as there will not be any use 
even if u initialize and can only be used for the the ouput 
whose value to passed into the function......

for those who dont like too much reading...... it may help
 
Is This Answer Correct ?    0 Yes 0 No
Rahul
 

 
 
 
Other C Sharp Interview Questions
 
  Question Asked @ Answers
 
Where does the dispose method lie and how can it be used to clean up resources? Wipro2
Any exceptions are there which are not caught by any catch blocks? what are they? Honeywell5
If we inherit a class do the private variables also get inherited ? TCS1
What are STA And MTA in threading? Kanbay2
How do you generate documentation from the C# file commented properly with a command-line compiler?  1
Difference between direct type casting and using "as" keyword? TCS1
What?s class SortedList underneath?  1
all information  1
What?s the difference between System.String and System.StringBuilder classes?  2
give suitable code for all login controls  1
What is difference between interface inheritance and class inheritance ? Digital-GlobalSoft5
Can you change the value of a variable while debugging a C# application?  1
Explain the Scope of public/private/friend/protected/protected friend. Wipro1
What does assert() do?  1
interface a { Method1() Method2() } class b: a { override Method1() override Method2() } what will happen & why?  4
What?s the difference between the System.Array.CopyTo() and System.Array.Clone()?  3
Hi to everybody. Lastweek i had taken an interview on c#. They ask what is boxing & unboxing, Masking.Please tell the answer and it is useful for me.  2
syntax for writing private method in a interface Wipro7
Is there any way to access private memebers of an object from another object? TCS4
What connections does Microsoft SQL Server support?  1
 
For more C Sharp Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com