Skip to content

Using a lambda expression in reassignment of itself. #2490

Answered by GalaxiaGuy
Blanen asked this question in Q&A
Discussion options

You must be logged in to vote

I'm not completely sure what you're expecting to happen, but I think the problem is not the lambda reassignment itself, but how lambdas capture variables (which in this case happens to be a lambda).

Does the following do what you expect:

using System;

public class Program
{
	public static void Main()
	{
		Func<int, int> add = (x) => x + 2;
		var temp = add;
		add = (x) => temp(x) + 2;
		Console.WriteLine(add(2));
	}
}

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YairHalberstadt
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #2490 on October 26, 2020 12:59.