R# makes invalid suggestion that forloop can be represented as LINQ when the loop contains ref or out parameters.

Consider the following two methods which are being used within some game logic:

        public int CountLiberties(Stone stone)         {             if(stone.Player == -1) return 1;             int count = 0;             int player = stone.Player;             var visited = new List<Stone>                               {                                   stone                               };             foreach(var neighbor in GetNeighbors(stone))             {                 count += CountLiberties(neighbor, ref visited, player);             }             return count;         }         private int CountLiberties(Stone stone, ref List<Stone> visited, int player)         {             if (!visited.Contains(stone))             {                 visited.Add(stone);                 if (stone.Player < 0)                 {                     return 1;                 }                 if (stone.Player == player)                 {                     int count = 0;                     foreach (var neighbor in GetNeighbors(stone))                     {                         count += CountLiberties(neighbor, ref visited, player);                     }                     return count;                 }             }             return 0;         }



Resharper makes the suggestion that each of the foreach loops can be replaced with a LINQ expression. The resulting code if each suggestion is used is as follows:

        public int CountLiberties(Stone stone)         {             if(stone.Player == -1) return 1;             int player = stone.Player;             var visited = new List<Stone>                               {                                   stone                               };             return GetNeighbors(stone).Sum(neighbor => CountLiberties(neighbor, ref visited, player));         }         private int CountLiberties(Stone stone, ref List<Stone> visited, int player)         {             if (!visited.Contains(stone))             {                 visited.Add(stone);                 if (stone.Player < 0)                 {                     return 1;                 }                 if (stone.Player == player)                 {                     return GetNeighbors(stone).Sum(neighbor => CountLiberties(neighbor, ref visited, player));                 }             }             return 0;         }



The first suggestion is valid, but the second instance results in a compile error.
"Cannot use ref or out parameter 'visited' inside an anonymous method, lambda expression, or query expression."
2 comments
Comment actions Permalink

Hello Nicholas
     Thank you for reporting this problem! I've logged it under http://youtrack.jetbrains.com/issue/RSRP-301353 and you're welcome to monitor the status of this request.

Andrey Serebryansky

Senior Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"

0
Comment actions Permalink

Since you're only adding items to the list, and not replacing it with a new list instance, you don't need the "ref" modifier on that parameter.

0

Please sign in to leave a comment.