I'm attempting to see if I can make the following code work with non null-able types. I recently discovered that value types act "strangely" when used as ref
parameters - they can be null..
Is it possible to return an "assignment target" from a function such that the scenario described by the following code may be realized?
void Main()
{
// want to call F and assign the result to _result
CallAndAssign(() => F(), () => _result);
}
int F()
{
return 1;
}
int _result;
void CallAndAssign<T>(Func<T> functionToCall, Func<T> memberToAssignResultTo)
{
// As I suspected, this did not work, "left-hand side of an assignment must be a variable, property or indexer"
//memberToAssignResultTo() = functionToCall();
}
I realize I could do this by making the second parameter an Action and simply perform the assignment, but that's not really different than doing the same thing on the next line of code.
My intuition is that, if this is possible, then it will involve the use of Expression, one of the last C# topics I've yet to wrap my brain around to a useful degree.