Skip to main content

Posts

Showing posts with the label ref

C# : Why Ref and Out keywords are different?

In the realm of C# programming, the  ref  and  out  keywords serve as powerful tools for handling parameters in method calls.  While they might seem similar, each plays a distinct role. In this blog post, we'll delve into the nuances of these keywords, exploring their applications and providing illuminating code snippets. ref Keyword: Passing by Reference The  ref  keyword allows a method to modify the value of the parameter it receives. It facilitates two-way communication between the calling method and the called method. public void IncrementByRef ( ref int number ) { number ++ ; } // Usage int value = 5 ; IncrementByRef ( ref value ) ; Console . WriteLine ( value ) ; // Output: 6 C# Copy In this example, the  IncrementByRef  method modifies the value of the  number  parameter, and the change is reflected in the calling method. out Keyword: Returning Multiple Values The  out  keyword is used to pass ...