6

I'm experimenting with C# Expression Trees and trying to understand the difference between Expression.Return and Expression.Goto. I can’t create an example where Return and Goto behave differently inside a block. I want to clearly understand when to use one over the other in expression tree generation.

Example

[Fact]
public void Block_Return_LikeGoto()
{
  var type = typeof(int);
  var label1 = Expression.Label(type);
  var label2 = Expression.Label(type);
  var body = Expression.Block(
    Expression.Return(label1, Expression.Constant(1)),
    Expression.Label(label1, Expression.Constant(2)),
    Expression.Label(label2, Expression.Constant(3)));

  var func = Expression.Lambda<Func<int>>(body).Compile();

  Assert.Equal(3, func());
}

Here, Return does not exit the block immediately; the label’s value is returned at the end of the block, so the behavior is the same as using Goto.

  1. When exactly do Return and Goto behave differently?

  2. Are there cases where Goto can never replace Return?

New contributor
Neomaster is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

8

Goto, Break, Continue and Return all create Goto statements, but with different Kind properties. In other words, they all do the same thing, but something processing the expression tree can find out what sort of jump it was, semantically.

See e.g. the docs for Expression.Return:

Returns

A GotoExpression with Kind equal to Return, the Target property set to target, and a null value to be passed to the target label upon jumping

If we look to see what code cares about the Kind property, the answer is not much. Just Expression.ToString() and similar string-creating methods.

It might be that other 3rd-party libraries care about the distinction, in which case they can check the Kind property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.