Create two properties defined using C# 6+ lambda expression syntax - CSharp Custom Type

CSharp examples for Custom Type:Property

Description

Create two properties defined using C# 6+ lambda expression syntax

Demo Code

using static System.Console;
using System;/*from   www . j  av  a 2 s . c  o  m*/
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      var sam = new Person
      {
         Name = "Sam",
         DateOfBirth = new DateTime(2020, 1, 27)
      };
      WriteLine(sam.Origin);
      WriteLine(sam.Greeting);
      WriteLine(sam.Age);
   }
}
public class Person : object
{
   public string Name;
   public DateTime DateOfBirth;
   public List<Person> Children = new List<Person>();
   public readonly DateTime Instantiated;
   public const string Species = "Programmer";
   public readonly string HomePlanet = "Earth";
   public Person()
   {
      Name = "Unknown";
      Instantiated = DateTime.Now;
   }
   public string Origin
   {
      get
      {
         return $"{Name} was born on {HomePlanet}";
      }
   }
   // two properties defined using C# 6+ lambda expression syntax
   public string Greeting => $"{Name} says 'Hello!'";
   public int Age => (int)(System.DateTime.Today .Subtract(DateOfBirth).TotalDays / 365.25);
}

Result


Related Tutorials