Tuesday, October 15, 2019

How Ref is different from Out parameter in C#?

Ref and Out both are used to pass the argument through the methods. The only main difference between these two are initialization. 'Ref' Keyword must have initialize the value or object before calling the function and in 'Out' Keyword there is not necessary to initialize before calling but it must be initialized in called method

Example

In this example two methods are called in the main function. In one method have ref keyword and in other have out keyword.


The Ref keyword gave us the compile time error because as it doesn't have initialized when it is calling. In case of Out we must need to initialized the value in called method before any operation take place.

No comments:

Post a Comment