How to make my Action working?

I have code below: I get error like "Operator '.' cannot be applied to operand of type 'void'". Is there anybody can explan the reason and modify the code please?

void Main()
{
    Claims.AsEnumerable().Select((claim, no) => Populate(claim, no)).ToList().ForEach(Modify).Dump();;
}

public Func<Claim, int, string> Populate = (claim, no) =>
{
    return "#" + no.ToString() + " " + claim.Applicant.FirstName + " " + claim.Applicant.LastName;
};


public Action<string> Modify = p =>
{
    p = p + "!!!";
};
Jon Skeet
people
quotationmark

This is the problem:

....ForEach(Modify).Dump

ForEach returns void. You could rewrite it as:

var list = Claims.AsEnumerable()
                 .Select((claim, no) => Populate(claim, no))
                 .ToList();
list.ForEach(Modify);
list.Dump();

However, then you'll see that you aren't actually modifying the list at all - the code in Modify() changes the parameter value, but that won't change what's in the list. Each value in the list (each string reference) is passed to the action in turn. The value is copied into the parameter, and you then change the parameter value... but that isn't connected to the list. This isn't really LINQ-related - it's simple C#. For example:

string x = "hello";
Modify(x);
Console.WriteLine(x); // hello

See my article on parameter passing for more details.

In general, LINQ encourages you not to try to make in-place modifications though. It would be cleaner to change Modify to a Func<string, string> and use Select - no list required:

Claims.AsEnumerable()
      .Select((claim, no) => Populate(claim, no))
      .Select(Modify)
      .Dump();
...
public Func<string, string> Modify = p =>
{
    p = p + "!!!";
    return p;
};

Or more simply for Modify:

public Func<string, string> Modify = p => p + "!!!";

(As an aside, I'd also strongly discourage you from using public fields, but that's somewhat by-the-by here.)

people

See more on this question at Stackoverflow