Reputation: 4318
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.
Upvotes: 25
Views: 47072
Reputation: 23976
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.
Upvotes: 13
Reputation: 1063338
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:
Upvotes: 59
Reputation: 564641
Parameters in C# are passed "ByVal" by default. You have to specify "ref" or "out" if you want different behavior.
Upvotes: 7