Is it generally considered better to pass parameters as pointers rather than as value when you can? Obviously it largely depends on the situation, but when there is a choice, is it better to use pointers?
Is this simply for reasons of memory?
And what is better to pass through if it is true, a pointer or a reference?
Out Parameters. Default Parameters or Optional Arguments (C# 4.0 and above) Dynamic parameter (dynamic keyword). Value parameter or Passing Value Types by Value (normal C# method param are value parameter)
We can use all 4 types of parameters in one function.
In a method or constructor invocation or class instance creation expression, argument expressions may appear within the parentheses, separated by commas. Each argument expression appears to be fully evaluated before any part of any argument expression to its right.
Some general rules of thumb:
If you need to modify it, pass a pointer or a reference. If the value might be null, pass a pointer, otherwise pass a reference.
If it's large, pass a const pointer or const reference, depending on whether null is a legal value.
If using pointers, prefer smart pointers to bare pointers.
Otherwise, pass by value.
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