This blog post for the beginner, those who have started his career in .Net technologies.
It's a common question in the interview interviewer and many have a misconception and given the wrong answer during the interview.
In .NET Framework, all objects are by default passed by value not passed by reference either it is a Value Type (Primitive types like int, char, double, etc.) or Reference Type (class, interface, delegate, string, etc.).
Let's see the example and clear the doubt.
1. Pass By Value
- Already, mention all objects passed by value default. So if you create any method and pass any parameters. It's a Pass By Value default.
That means, in other words, It will copy value one object to another object. Check the example of a Primitive type.
int x = 5; int y = 5; Cal(x, y); Console.WriteLine(x + y); public void Cal(int x, int y) { x = 10; y = 6; Console.WriteLine(x + y); }
Here you can see, we have pass parameter x and y into Cal method as Pass By Value.
The default value of x and y variable is 5. When we call Cal method and assign values inside the method to x, y to 10 and 6. After assigning the new value and print the value it will be the sum of x,y = 16.
After calling Cal method then we have another Console.WriteLine that result will be for the sum of x,y = 10 because of when we pass the parameter values to Cal method it will copy the value to x,y to the parameters of x,y.
So when we reassign values to x,y inside the method it will not change the values of outside method variables of x,y.
Check the example of Reference Type.
var user = new User { FirstName = "Kalpesh", LastName = "Satasiya" }; ShowUserName(user); Console.WriteLine(user.FirstName +" "+ user.LastName); public void ShowUserName(User user) { user.LastName = "S"; Console.WriteLine(user.FirstName +" "+ user.LastName); }
For the above example, we have to create User class with FirstName and LastName properties.
We want to show username so for that created ShowUserName method and pass user object as a Pass By Value same as the above primitive example.
After creating the object and assign the values FirstName="Kalpesh" and LastName="Satasiya".
Now calling ShowUserName method and passing user object. So user object value copies to ShowUserName method parameter.
Inside the ShowUserName method, we are reassigning the value to LastName="S".
Time to print the user name and it will print the "Kalpesh S" as an output of Console.WriteLine.
Still, we have one more Console,WriteLine statement pending to execute and that output will be the "Kalpesh Satasiya".
So when you pass an object as a parameter. It does not mean to change the value of object inside the method will reflect outside method variables.
It will just copy the object value and assign to parameter object value.
2. Pass By Reference
- Pass By Reference means it will passing the reference of the memory location.
Lets understanding with examples of Pass By Reference of Primitive type.
int x = 5; int y = 5; Cal(ref x, ref y); Console.WriteLine(x + y); public void Cal(ref int x, ref int y) { x = 10; y = 6; Console.WriteLine(x + y); }
In the above example, you can see it. We have used the same example which we have used on Pass By Value with few modifications.
Here we are calling Cal method with x,y parameters and also mention the "ref" keyword with parameters that it means we are passing the reference, not the values.
Once we are calling the Cal method and inside the method, we are doing to change the value of x,y. It will also reflect the values of outside the Cal method variables of x,y.
Console.WriteLine statement of Cal method will print an output the sum of x,y = 16.
Also, the Same output will be the print of outside the Console.WriteLine statement.
For Pass By Reference of Reference type object.
var user = new User { FirstName = "Kalpesh", LastName = "Satasiya" }; ShowUserName(ref user); Console.WriteLine(user.FirstName +" "+ user.LastName); public void ShowUserName(ref User user) { user.LastName = "S"; Console.WriteLine(user.FirstName + " " + user.LastName); }
The Same example we are going to used which we have used in Pass By Value of Reference type with few modifications.
You can see only modifications are added "ref" keyword into the parameter.
When we calling the ShowUserName at time user object with ref keyword as well also added ref keyword at ShowUserName method implementation with user parameter.
Let's check the output of both the Console.WriteLine statement.
ShowUserName method Console.WriteLine output is "Kalpesh S".
Also, the same out of outside the method Console.WriteLine statement is "Kalpesh S".
Why both out is same? because we are passing the reference object as the parameter into the method so inside the method any changes on user object properties will be reflected on the passing object outside the method.
Hope it will help you to clear the concept of Pass By Value and Pass By Reference in C#.
0 comments:
Post a Comment