What are the main differences, if any, of Python’s argument passing rules vs C#’s argument passing rules?
I’m very familiar with Python and only starting to learn C#. I was wondering if I could think of the rule set as to when an object is passed by reference or by value the same for C# as it is in Python, or if there are some key differences I need to keep in mind.
C# passes parameters by value unless you specify that you want it differently. If the parameter type is a struct, its value is copied, otherwise the reference to the object is copied. The same goes for return values.
You can modify this behavior using the
reforoutmodifier, which must be specified both in the method declaration and in the method call. Both change the behavior for that parameter to pass-by-reference. That means you can no longer pass in more complex expressions. The difference betweenrefandoutis that when passing a variable to arefparameter, it must have been initialized already, while a variable passed to anoutparameter doesn’t have to be initialized. In the method, theoutparameter is treated as uninitialized variable and must be assigned a value before returning.