Pass reference type using "ref"

By FoxLearn 12/27/2024 2:52:41 AM   130
When working with reference types in C#, passing them to methods allows modifications to the object’s properties within the method.

However, there is a distinction when using the ref keyword. While reference types inherently pass by reference, the ref keyword offers additional functionality.

What Happens When ref Is Used?

When a reference type object is passed to a method, changes made to its properties inside the method affect the original object, even without the ref keyword. The ref keyword becomes important when you want to replace the object with a completely new one inside the method, and have that change reflected in the calling code.

Let’s take a look at an example using a Book object:

using System;

class Program
{
    static void Main()
    {
        // Creating a Book object
        Book b = new Book("C# Programming", "John Doe");
        
        // (1) Without using ref
        ModifyWithoutRef(b);
        // OUTPUT: Title: Advanced C#, Author: John Doe
        Console.WriteLine($"Title: {b.Title}, Author: {b.Author}");
        
        // (2) Using ref
        ModifyWithRef(ref b);
        // OUTPUT: Title: Java Programming, Author: Chris Lee
        Console.WriteLine($"Title: {b.Title}, Author: {b.Author}");
    }

    // Method to modify object properties without using ref
    static void ModifyWithoutRef(Book book)
    {
        // Modifying properties of the object
        book.Title = "Advanced C#";
        
        // Replacing the object, but this change won't affect the caller
        book = new Book("C++ Programming", "Alex Smith");
    }

    // Method to modify object properties using ref
    static void ModifyWithRef(ref Book book)
    {
        // Modifying properties of the object
        book.Title = "Mastering C#";
        book.Author = "Jane Doe";
        
        // Replacing the object, and this change will be reflected in the caller
        book = new Book("Java Programming", "Chris Lee");
    }
}

class Book
{
    public string Title { get; set; }
    public string Author { get; set; }

    public Book(string title, string author)
    {
        this.Title = title;
        this.Author = author;
    }
}

In this example:

  • Without ref:

    • The ModifyWithoutRef method modifies the Book object’s properties, but when the object is replaced inside the method (with a new Book instance), the change does not reflect outside the method. This is because the book parameter is passed by reference, but the reference itself is not updated in the calling code.

    • Output: Title: Advanced C#, Author: John Doe

  • With ref:

    • The ModifyWithRef method does the same initial modifications to the Book object, but it also replaces the object inside the method using the ref keyword. Since ref allows the reference itself to be modified, the caller now receives the new object after the method executes.

    • Output: Title: Java Programming, Author: Chris Lee

The key difference between using ref and not using ref with reference types is that ref allows both the properties of the object and the reference itself to be modified inside the method. Without ref, only the object's internal state can be changed, and the object reference itself remains unchanged in the caller’s scope.