I have written this blog for Ref keyword though there are many blogs and articles available around this. But I have noticed some very specific behavior when dealing with Ref keyword.
In C#, variables are of two types - Value Type and Reference Type. Ref keyword can be used for these two types which means we are handling these types by their references.
There are some differences when we are using with Ref and without Ref. To use the Ref keyword, there are three rules (these are the main differences with the Out keyword),
- Rule 1: Variable should be initialized before use
- Rule 2: Call the method with ref keyword in prefix
- Rule 3: Receive variable in method with ref keyword in prefix
Let’s see some code here and try to understand the differences.
Value Type with Ref keyword
- class Program {
- static void Main(string[] args) {
- int valueTypeA = 10; // Rule 1: Variable should be initialized before use
- MethodWithOutRef(valueTypeA);
- Console.WriteLine(valueTypeA); //Output: 10, As the modification was in local scope of MethodWithOutRef
- MethodWithRef(ref valueTypeA); // Rule 2: Call the method with ref keyword in prefix
- Console.WriteLine(valueTypeA); //Output: 12, Here is the magic of Ref
- Console.Read();
- }
- static void MethodWithOutRef(int recVar) {
- recVar = 12; //modifiy the value to 12
- }
- static void MethodWithRef(ref int recVar) // Rule 3: Receive variable in method with ref keyword in prefix
- {
- recVar = 12; //modify the value to 12
- }
- }
Code Explanation
As we see that with Ref keyword, the output is 12, this is because we passed the reference. Both valueTypeA and recVar are pointing to the same variable that’s why recVar modify the actual value.
Reference Type with Ref keyword
Reference Type has a little bit more complex mechanism than Value Type. Whenever we are passing the Reference Type (Object) in any method that means we are passing the reference.
- When we are passing any Reference Type that means we are passing a reference (Pointer) of the actual object.
- When we are passing any Reference Type with Ref keyword that means we are passing the actual object (Pointer to pointer).
What does this mean? Let’s see in the example.
- class Program {
- static void Main(string[] args) {
- var obj = new TestReferenceType {
- IntProp = 10, StrProp = "First"
- }; // Rule 1: Object should be initialized before use
- MethodWithOutRef(obj);
- Console.WriteLine(obj.IntProp); //Output : 12
- Console.WriteLine(obj.StrProp); //Output : Second
- MethodWithRef(ref obj); // Rule 2: Call the method with ref keyword in prefix
- Console.WriteLine(obj.IntProp); //Output : 14
- Console.WriteLine(obj.StrProp); //Output : Third
- Console.Read();
- }
- static void MethodWithOutRef(TestReferenceType recVar) {
- recVar.IntProp = 12; //modifiy the value to 12
- recVar.StrProp = "Second"; //modify the value to Second
- }
- static void MethodWithRef(ref TestReferenceType recVar)
- // Rule 3: Receive variable in method with ref keyword in prefix
- {
- recVar.IntProp = 14; //modify the value to 14
- recVar.StrProp = "Third"; //modify the value to Third
- }
- }
- class TestReferenceType {
- public int IntProp {
- get;
- set;
- }
- public string StrProp {
- get;
- set;
- }
- }
Join the conversation! Your thoughts help the community grow.