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

what happens if you inherit multiple interfaces and they have conflicting method names?

0 Answers   Siebel Systems,


Define collections?

0 Answers  


What is hashtable in c# with example?

0 Answers  


What do you mean by thread safe in c#?

0 Answers  


What does mean before a string in c#?

0 Answers  






When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)?

2 Answers   Mind Tree,


What is default value of enum c#?

0 Answers  


What does console writeline do?

0 Answers  


Is c# good for games?

0 Answers  


Is array a collection c#?

0 Answers  


What is immutable in C#?

0 Answers   SwanSoft Technologies,


What is difference between dictionary and list in c#?

0 Answers  


Categories