In C#, the ref keyword is used to indicate that a method parameter is a reference parameter. Reference parameters allow you to pass a reference to a variable as an argument to a method, rather than passing a copy of the variable’s value. This means that any changes made to the parameter within the method will affect the original variable outside the method as well.

Example

In below we have defined the syntax of ref method. The ref method have retrun type for example int, float or string and a methodName that can be any suitable name of the method and we have defined the parameter ref with the first ref keword then it will have any datatype for example int, string and parameter name for example referenceParameter in below syntax then we have defined the body of parameter where we used this ref parameter and will assigne a value to this ref parameter.

returnType methodName(parameters, ref dataType referenceParameter)
{
// Method body
// The method assigns a value to referenceParameter
// ...
}

Basic structure of ref parameter

Method Signature:

When defining a method that accepts a ref keyword, you specify the ref keyword before the parameter type in the method signature.

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

Responses (1)