Ref and Out

-> Parameters are always passed by value to a method by default. If we want to pass them by reference then we can use either out or ref keyword.
-> The effect of passing by reference is that any change to the parameter in the called method is reflected in the calling method.
-> Reference parameters basically never pass the value of a variable used in the method calling; instead they use the variable themselves. Rather than creating a new storage for that variable in the method declaration, the very same storage space is used, so the value of the variable in the member method and the value of the reference parameter will always be the same. Reference parameter requires ref modifier as part of both declaration and the calling.
-> Output parameters are very much like reference parameters. The variable specified at the time of calling doesn’t need to have been assigned a value before it’s passed to the called method. When the method is invoked completely we can read that variable as it will be assigned.
-> An argument that is passed using a ref keyword must be initialized in the calling method before it’s passed to the called method.
-> An argument that is passed as an out keyword can be passed without assigning any value to it and must be initialized in the called method before it returns back to calling method.
-> Both ref and out cannot be used in method overloading simultaneously, however method overloading can be done, if one method takes a ref or out argument and the other method takes simple argument.

Leave a Reply

Your email address will not be published. Required fields are marked *