As I understand it, C# passes parameters into methods by reference. In VB.NET, you can specify this with ByVal and ByRef. The default is ByVal.
Is this for compatibility with Visual Basic 6.0, or is it just random? Also, how can I specify what to use in C#? I kind of like the idea of passing parameters by value.
ByRef = You give your friend your term paper (the original) he marks it up and can return it to you. ByVal = You give him a copy of the term paper and he give you back his changes but you have to put them back in your original yourself. As simple as I can make it.
ByVal stands for By Value i.e. when the subprocedure called in from the procedure the value of the variables is reset to the new value from the new procedure called in.
Forces a parameter to be passed by value rather than by reference. The ByVal keyword can appear before any parameter passed to any function, statement, or method to force that parameter to be passed by value. Passing a parameter by value means that the caller cannot modify that variable's value.
C# Passing arguments by default is ByRef instead of ByVal.
Parameters in C# are, by default passed by value. There is no modifier to make this explicit, but if you add ref
/ out
the parameter is by-reference.
The usual confusion here is the difference between:
Passing by value is the default in C#. However, if the variable being passed is of reference type, then you are passing the reference by value. This is perhaps the origin of your confusion.
Basically, if you pass a reference by value, then you can change the object it refers to and these changes will persist outside the method, but you can't make variable refer to a different object and have that change persist outside the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With