Ref locals

The ref local keyword allows you to store references in a local variable by declaring local variables with the ref keyword and add the ref keyword before a method call or assignment. For example, in the following code, the day string variable references dayOfWeek; changing the value of day also changes the value of dayOfWeek and vice versa:

string dayOfWeek = "Sunday";ref string day = ref dayOfWeek;Console.WriteLine($"day-{day}, dayOfWeek-{dayOfWeek}");day = "Monday";Console.WriteLine($"day-{day}, dayOfWeek-{dayOfWeek}");dayOfWeek = "Tuesday";Console.WriteLine($"day-{day}, dayOfWeek-{dayOfWeek}");-----------------Output:day: SundaydayOfWeek:  Sundayday: MondaydayOfWeek:  Mondayday: TuesdaydayOfWeek:  Tuesday

Get C# and .NET Core Test Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.