Difference between ByVal and ByRef?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
If i have 100 objects in my application are out of scope.when first time garbage collected how many objects memory reference are free?
What is token in c#?
How would you describe encapsulation in c#?
How?s method overriding different from overloading?
What is the use of system.environment class in c#.net?
What is file extension of webservices?
What is sqlcommand in c#?
Define constructors
Can multiple catch blocks be executed?
Explain About multi level and multiple inheritance how to achieve in .net
What issues can be faced while delivering code?
How do you concatenate in c#?