Training - Beratung - Projektarbeiten

www.David-Tielke.de

Webcast C# 6.0 - Folge 3: Expression Bodied Function Member

Immer dann, wenn in C# eine Methode oder ein Computed-Property mit nur einem einzigen Statement geschrieben wird, erzeugt dies, bedingt durch den notwendigen Anweisungsblock nach Methoden oder Properties, unnötig viel Code. Ein ähnliches Problem existieren bei den in C# 2.0 eingeführten anonymen Funktionen. In C# 3.0 wurde deshalb mit den Lambdaausdrücken eine Kurzform hinzugefügt, bei der mit Hilfe eines Projektionsoperators, sehr kurze und knappe “Inlinemethoden” geschrieben werden können:

names.Where(n => n.Age >= 18);

Mit C# 6.0 und den “Expression Bodied Function Member”, existiert nun eine solche Variante auch für die direkte Deklarationen von Methoden und berechneten Eigenschaften.

Dazu aus der Projekt Roslyn Webseite:

Lambda expressions can be declared with an expression body as well as a conventional function body consisting of a block. This feature brings the same convenience to function members of types. Expression bodies on method-like members Methods as well as user-defined operators and conversions can be given an expression body by use of the “lambda arrow”:
public Point Move(int dx, int dy) => new Point(x + dx, y + dy); 
public static Complex operator +(Complex a, Complex b) => a.Add(b);
public static implicit operator string(Person p) => p.First + " " + p.Last;
The effect is exactly the same as if the methods had had a block body with a single return statement. For void-returning methods – and Task -returning async methods – the arrow syntax still applies, but the expression following the arrow must be a statement expression (just as is the rule for lambdas):
public void Print() => Console.WriteLine(First + " " + Last);
Expression bodies on property-like function members Properties and indexers can have getters and setters. Expression bodies can be used to write getter-only properties and indexers where the body of the getter is given by the expression body:
public string Name => First + " " + Last;
public Customer this[long id] => store.LookupCustomer(id); 
Note that there is no get keyword: It is implied by the use of the expression body syntax.
Links

Webcast C# 6.0 – Folge 2: Read Only Auto Properties

Einer der Erweiterungen in C# 6.0, die sehr viel boilerplated Code vermeiden können, sind die Read Only Auto Properties.

Im zweiten Teil der Webcastreihe zu den Erweiterungen aus C# 6.0, widmen wir uns dieser genialien kleinen Erweiterung und zeigen wo sie eingesetzt werden sollte und wo nicht.

Dazu aus der Dokumentation des Roslyn-Projektes:

Auto-properties can now be declared without a setter.
public class Customer
{
    public string First { get; } = "Jane";
    public string Last { get; } = "Doe";
}
The backing field of a getter-only auto-property is implicitly declared as readonly (though this matters only for reflection purposes). It can be initialized through an initializer on the property as in the example above. Also, a getter-only property can be assigned to in the declaring type’s constructor body, which causes the value to be assigned directly to the underlying field:
public class Customer
{
    public string Name { get; }
    public Customer(string first, string last)
    {
        Name = first + " " + last;
    }
}
This is about expressing types more concisely, but note that it also removes an important difference in the language between mutable and immutable types: auto-properties were a shorthand available only if you were willing to make your class mutable, and so the temptation to default to that was great. Now, with getter-only auto-properties, the playing field has been leveled between mutable and immutable.

 

Links

Webcast C# 6.0 - Folge 1: Auto Property Initializers

Ab heute versuche ich regelmäßig Webcasts zu aktuellen Themen auf meinem YouTube-Channel zu veröffentlichen. Das Startthema werden die Neuerungen in C# 6.0 sein, welche ich in ~10 Episoden beleuchten werde.

Heute gibt es den ersten Teil zum Thema "Auto Property Initializers".


Dazu aus der Beschreibung des Roslyn-Teams:

You can now add an initializer to an auto-property, just as you can in a field:
public class Customer
{
    public string First { get; set; } = "Jane";
    public string Last { get; set; } = "Doe";
}
The initializer directly initializes the backing field; it doesn’t work through the setter of the auto-property. The initializers are executed in order as written, just as – and along with – field initializers. Just like field initializers, auto-property initializers cannot reference this – after all they are executed before the object is properly initialized.

Links: