-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

C# 6 and .NET Core 1.0

Earlier, you created a method named GetOrigin
that returned a string
containing the name and origin of the person. Languages such as Java do this a lot. C# has a better way: properties.
A property is simply a method (or pair of methods) that act like a field when you want to get or set a value, thereby simplifying the syntax.
In the Person2.cs
file, inside the Person
class, add the following code to define three properties.
The first property will perform the same role as the GetOrigin
method, using the property
syntax that works with all versions of C# (although it uses the C# 6-only string interpolation syntax).
The second property will return a greeting message using the new C# 6 lambda expression (=>
) syntax.
The third property will calculate the person's age.
Here is the code:
// property defined using C# 1 - 5 syntax public string Origin { get { return $"{Name} was born on {HomePlanet}"; }...
Change the font size
Change margin width
Change background colour