I know that this code is not good and maybe it's stupid question but can anybody explain me why it works in this way?
I have this simple class
public class Customer
{
public string Name { get; set; }
}
And I have next method with Action delegate
private Customer GetCustomer(Action<Customer> action)
{
var model = new Customer { Name = "Name 1" };
action?.Invoke(model);
return model;
}
Then I execute this method with delegate and write into console model.Name
var model = GetCustomer(c => c = new Customer { Name = "Name 2" });
Console.WriteLine(model.Name);
I expected to get "Name 2". But I get "Name 1" - which is value of the class defined inside method. I understand that to get what I want - I could write code in this way:
var model = GetCustomer(c => c.Name = "Name 2");
Console.WriteLine(model.Name);
And everything will be ok. But why my first implementation doesn't work? I would be thankful a lot if somebody explain me this.