Travel property

OK. Took me a couple of listens to understand the travel property and, when I did, it was simple (isn’t it always?).

Declaring:

private Vector2 travel => (Vector2)cam.transform.position - startPos;

is exactly the same as:

private Vector2 GetTravel()
{
   return (Vector2)cam.transform.position - startPos;
}
4 Likes

That’s right. In C#, you can use the arrow notation => to ‘simplify’/shorten return expressions like that.

In the case of travel, it actually is a property. So the expanded version would actually be:

private Vector2 travel { get { return (Vector2)cam.transform.position - startPos; } }

And properties are basically shorter getter and/or setter methods, so it indeed behaves like the method you described.

Methods can actually also use => notation. So your method:

private Vector2 GetTravel()
{
   return (Vector2)cam.transform.position - startPos;
}

can also be written as:

private Vector2 GetTravel() => (Vector2)cam.transform.position - startPos;

Notice the difference between the travel property and the GetTravel() method when using => notations? A property doesn’t have parentheses, and a method does.

Which notation should you use? That’s all up to you! If you’re working in a team, discuss with your team. If you’re working solo, do whatever feels right to you.

1 Like

Privacy & Terms