Ref returns

You can also use the ref keyword as a return type of methods. To achieve this, add the ref keyword to the method signature and inside the method body, add ref after the return keyword. In the following code snippet, an array of string is declared and initialized. The fifth element of the string array is then returned by the method as a reference:

public ref string GetFifthDayOfWeek(){    string [] daysOfWeek= new string [7] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};    return ref daysOfWeek[4];}

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.