Pass reference type using "ref"
By FoxLearn 12/27/2024 2:52:41 AM 130
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 theBook
object’s properties, but when the object is replaced inside the method (with a newBook
instance), the change does not reflect outside the method. This is because thebook
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 theBook
object, but it also replaces the object inside the method using theref
keyword. Sinceref
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.
- Using the OrderBy and OrderByDescending in LINQ
- Querying with LINQ
- Optimizing Performance with Compiled Queries in LINQ
- MinBy() and MaxBy() Extension Methods in .NET
- SortBy, FilterBy, and CombineBy in NET 9
- Exploring Hybrid Caching in .NET 9.0
- Using Entity Framework with IDbContext in .NET 9.0
- Primitive types in C#