Demonstration of String Interpolation inside Statement Lambda using C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1102
The below code will show the way to use String Interpolation inside Statement Lambda using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var personData = new List<PersonMaster>();

//populating some data
Enumerable
.Range(1, 10)
.ToList()
.ForEach(
i =>
personData.Add(new PersonMaster
{
PersonID = i,
PersonName = string.Concat("Person ", i),
Age = 14+i
})
);

//Use of Statement Lambda
var eligiblePersonData = new List<EligiblePerson>();

personData
.ToList()
.ForEach(i =>
{

switch (i.Age < 18 ? 0 : 1)
{
case 0:

//note the usage of String Interpolation - a feature of C# 6.0

string toDisplay = $"Mr. {i.PersonName} is not eligible to case vote as his age is {i.Age} which is under 18";
Console.WriteLine(toDisplay);
break;
case 1:
eligiblePersonData.Add(new EligiblePerson
{
PersonID = i.PersonID,
PersonName = i.PersonName,
Age = i.Age
});
break;
}
});

Console.ReadKey();
}
}

//The main person model
public class PersonMaster
{
public int PersonID { get; set; }
public string PersonName { get; set; }
public int Age { get; set; }

}

//Eligible Person Model
public class EligiblePerson
{
public int PersonID { get; set; }
public string PersonName { get; set; }
public int Age { get; set; }
}
}

Comments or Responses

Login to post response