Difference between ByVal and ByRef?

Answers were Sorted based on User's Feedback



Difference between ByVal and ByRef?..

Answer / ashwini

When arguments are passed to a method ByVal i.e. By Value
then the original value of a variable is not passed but its
copy is created and that copy is passed. So whatever
changes are made, they will affect the copy but not the
original value of a variable.
When arguments are passed ByRef i.e. By Reference
then the argument's memory location is passed to a method.
So changes made to that argument's value will actually
affect the original value.

Is This Answer Correct ?    31 Yes 1 No

Difference between ByVal and ByRef?..

Answer / bhaskar

ByVal means only the value is passed to the calling function
but.. modifications will not reflect the original value..

ByRef means the address where the value stored is passes to
the calling function so the modifications made are reflected
to the original value..

Is This Answer Correct ?    10 Yes 0 No

Difference between ByVal and ByRef?..

Answer / priya

If you want to pass the value of the variable, use the
ByVal syntax. By passing the value of the variable instead
of a reference to the variable, any changes to the variable
made by code in the subroutine or function will not be
passed back to the main code. This is the default passing
mechanism when you don’t decorate the parameters by using
ByVal or ByRef.

If you want to change the value of the variable in the
subroutine or function and pass the revised value back to
the main code, use the ByRef syntax. This passes the
reference to the variable and allows its value to be
changed and passed back to the main code.

Is This Answer Correct ?    2 Yes 1 No

Difference between ByVal and ByRef?..

Answer / bharat

Hi All,By theoretical definition, it's true if we pass
parameter as value type we are passing a copy of value not
original copy.So changes are not reflect on original
value,now there is a catch what if i pass "Reef
type"(Classes,Delegates,Interface etc) as value type

e.g.
//------------------By Val
tryFunction TF1 = new tryFunction();
TF1.Age = 20;
TestFunction(TF1);
Response.Write(TF1.Age.ToString());
void TestFunction1(ref tryFunction tf)
{
tf.Age = 35;
}

public class tryFunction
{
private int age;
public int Age
{
set
{
age=value;
}
get{return age;}
}

-----------------------------------
Then what should be the return value.
According to concept it is 20,but on reality it is 35.So
where I am wrong?

Is This Answer Correct ?    0 Yes 3 No

Difference between ByVal and ByRef?..

Answer / richa

value type is in form of stack and reference typr is in
form of heap.

Is This Answer Correct ?    1 Yes 12 No

Post New Answer

More C Sharp Interview Questions

Can you drink alcohol with a loop recorder?

0 Answers  


What's c# ?

0 Answers  


Is xml tags are case sensitive?

0 Answers  


What is difference between constants and readonly in c#?

0 Answers  


Which are access modifiers available in c#?

0 Answers  






To whom a method is accesssed if it is marked as protected internal ?

0 Answers   Siebel,


What does exclamation mark mean in access query?

0 Answers  


Can we overload the main method in c#?

0 Answers  


What is the base class of all classes in c#?

0 Answers  


Which is faster hashtable or dictionary?

0 Answers  


List out two different types of errors in c#?

0 Answers  


What are access modifiers used for?

0 Answers  


Categories