By using 'ref' keyword we keeps the same address of two different variable.
without ref Scenario : When you does at normal functioning means without use of ref keyword, pass argument value from calling method to method defination at that time there is different addresses for both argument and parameter.
ref Scenario : ref parameter in method defination refers the same address of argument at calling method and parameter at method defination.
Below are some images that will explore ref keyword program with flow :
Look at Snap no. 1: Program will start from Main (..) method , here I am assigning a text to string variable.
argument value and address passing to method body parameter at class ref_keyword.
Look at Snap no. 5:
Now memory location value part will reflect as below [take refrence here from Snap no 3]:
Greetings !!! address [20000] remain same but value got changed.

Look at Snap no. 6:
returning changed value at same address ..
Look at Snap no. 7: Here we proved smart use of ref keyword
Output :
without ref Scenario : When you does at normal functioning means without use of ref keyword, pass argument value from calling method to method defination at that time there is different addresses for both argument and parameter.
ref Scenario : ref parameter in method defination refers the same address of argument at calling method and parameter at method defination.
Below are some images that will explore ref keyword program with flow :
Look at Snap no. 1: Program will start from Main (..) method , here I am assigning a text to string variable.
Look at Snap no. 2: From previous snap, there is wrong statement so, i want to correct that statement by using ref keyword.
Look at Snap no. 3: calling method
method(ref femaleVoiceIsLikeMale);
here, argumnet name femaleVoiceIsLikeMale followed by ref keyword, means that argument value goes into perticuler memory loaction picturized as below :
Female voice is exactly equal to Male voice : Wrong statement | - value |
200000 | - address |
Look at Snap no. 4: argument value and address passed to method body parameter, but here I am going to change the argument value that was passed from calling method as :
"Female voice is exactly different than Male voice : Correct statement"
Look at Snap no. 5:
Now memory location value part will reflect as below [take refrence here from Snap no 3]:
Female voice is exactly different than Male voice : Correct statement | - value |
200000 | - address |
Greetings !!! address [20000] remain same but value got changed.

Look at Snap no. 6:
returning changed value at same address ..
Look at Snap no. 7: Here we proved smart use of ref keyword
Output :
Copy Full Code:






No comments:
Post a Comment