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 enum have methods c#?

0 Answers  


how can include .netframeworl 2.0 in application setup

1 Answers  


What .exe means?

0 Answers  


Is static thread safe?

0 Answers  


What is shared inheritance

3 Answers   IGT,






What is the difference between list and array in c#?

0 Answers  


What is difference between float and decimal?

0 Answers  


What happens if the inherited interfaces have conflicting method names?

0 Answers  


To create a localized application which namespaces are necessary?

0 Answers   Siebel,


what is the syntax to inherit from a class in c#?

0 Answers   Siebel Systems,


Can we make a class private in c#?

0 Answers  


What is difference between out and ref in c#?

0 Answers  


Categories