6

I have a problem with my code in c# if someone could help resolve my problem.

In a function I am parsing a Xml file and saving it to a struct.

Then I try to retreive some info from said struct with a specific node id and my code fails with

"Cannot use ref or out parameter 'c' inside an anonymous method, lambda expression, or query expression"

Here is my code:

public void XmlParser(ref Point a, ref Point b, ref Point c)
{
     XDocument xdoc = XDocument.Load(XmlDirPath); 
     var coordinates = from r in xdoc.Descendants("move")
                        where int.Parse(r.Attribute("id").Value) == c.NodeID  // !! here is the error !!
                        select new
                        {
                              X = r.Element("x").Value,
                              Y = r.Element("y").Value,
                              Z = r.Element("z").Value, 
                              nID = r.Attribute("id").Value
                         };

     foreach (var r in coordinates)
     {
          c.x = float.Parse(r.X1, CultureInfo.InvariantCulture);
          c.y = float.Parse(r.Y1, CultureInfo.InvariantCulture);
          c.z = float.Parse(r.Z1, CultureInfo.InvariantCulture);
          c.NodeID = Convert.ToInt16(r.nID);
     }
}

public struct Point
{
    public  float x;
    public  float y;
    public  float z;
    public  int   NodeID;
}
Arjun Prakash
  • 669
  • 9
  • 23
Tagyoureit
  • 359
  • 1
  • 5
  • 18
  • 4
    As an aside: a) I'd recommend avoiding public fields; b) I'd recommend avoiding mutable structs; c) if you find yourself using `ref` a lot with a struct, perhaps you should have a class instead? (They're far from equivalent, but if you're using `ref` to avoid the performance hit of copying, then that's definitely something to consider.) – Jon Skeet Apr 17 '15 at 11:45
  • [Mutable structs are evil](http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil) – juharr Apr 17 '15 at 11:46

3 Answers3

9

Well, you're not allowed to use a ref or a out parameter in an anonymous method or a lambda, just as the compiler error says.

Instead you have to copy the value out of the ref parameter into a local variable and use that:

var nodeId = c.NodeID;
var coordinates = from r in xdoc.Descendants("move")
    where int.Parse(r.Attribute("id").Value) == nodeId
    ...
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
5

As suggested in other answers you have to copy the ref variable locally in your method. The reason why you have to do it is because lambdas/linq queries change the lifetime of variables that they capture causing the parameters to live longer than the current method frame as the value can be accessed after the method frame is no longer on the stack.

There is an interesting answer here that explains carefully why you can't use ref/out parameters in anonymous methods.

Community
  • 1
  • 1
codingadventures
  • 2,924
  • 2
  • 19
  • 36
2

You should pull the retrieval of the ID out of the anonymous method:

var nodeId = c.NodeID;

var coordinates = from r in xdoc.Descendants("move")
                           where int.Parse(r.Attribute("id").Value) == nodeId
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325