Ref and Out Parameter in C#

In C# values of basic data type such as int, float, char, etc … are send as pass-by-value and values of user define data types or object values are send by as pass-by-reference.
 However c# give the flexibility to send values of you basic data type as pass-by-reference too, for that ref key work is use .there is some situation when we did not pass a value to function but we want to get value for the function such as pass a number to the function and want to get its remainder and dividend from the function to handle this situation c# has a key word out that is use to pass value from a function to the caller function.
 Ref parameter in c#
 Use to send basic data type by pass-by-reference .ref variable can’t be used for initialization purpose.
 Ref variable is define in function definition and function call. Ref variable must me assign a value before the first call because the compiler will consider as valid reference and pass it to the calling function if we did not assign it value the parameter in the function will get a reference pointing to nothing thus this a programming error.(example1)
 Ref variable is also used to send object to a function as reference-by-reference. In c# object reference usually send reference-by-value mean the reference of a variable is copied to other variable they are pointing to same object but have their on copy of reference. The parameter variable cannot change the original variable reference.
 Sending reference-by-reference is method that both the real variable and the parameter variable as the same copy of reference, the parameter variable in the calling function can change the object pointed by the original variable or by variable in the caller function.(example2)
 Out parameter in c#
 Out parameter can only be used to pass a parameter value out of the function.
 Out can used without initialization but it must be assign a value before returning from the function for which the out parameter is used. this parameter can be used for initialization purpose.(example3) 
Ebook Download
View all
View all