I've been seeing it used in some places =>
when declaring attributes on a class. For example:
public class Escritor: IProfesion
{
public string Trabajo => "Copywriter";
public string PalabraFavorita { get; set; }
}
How does it =>
affect Trabajo
ya Escritor
?
That is Expression-bodied member or members with body expression (in Spanish).
This is a feature available since C# 6. This converts the property to
readonly
. The compiler translates the expression:A:
The advantage of using body expressions is that they reduce code considerably. After that there is no difference between the 2 previous examples.
It is also possible to use the expression
=>
from C# 7 onwards to assign the value:You can also use it in constructors: