While learning C# you will learn about the different ways variables can be passed as arguments to function. In this post, I will try to summarize the

Paul-Sebastian Codes

submited by
Style Pass
2021-10-19 16:00:10

While learning C# you will learn about the different ways variables can be passed as arguments to function. In this post, I will try to summarize the essential things you need to know about argument passing in C#, the way I learned them and they I made sense of them.

In case you don't know C#, in C# arguments can be passed to parameters either by value or by reference. Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment.

The default behavior, when no parameter modifiers are used, is called pass by value. What this means is that whatever the variable holds is copied from the passed-in variable to the function argument variable.

This can sometimes be confusing for new programmers, because they think that only primitive types, like int and double, are passed by value, because they can be copied easily and fairly inexpensively, so that's what C# does, but reference types can also be passed by value (copied) — but, it's the reference that gets copied, because that is what the variable holds (a reference or... a reference type).

Leave a Comment