I will pick Action and Func,
Example for Action, we are performing an task in a class which the caller a form, subscribes to the event which adds a string to a ListBox, to prevent cross threading we must use .Invoke.
The action in the extension method is lb => { listBox2.Items.Add(name);}
, in the extension method .Invoke adds the item.
// in the form
public async Task IterateContactNames()
{
await foreach (var name in DataOperations.GetAllNamesPaged(true))
{
listBox2.InvokeIfRequired(lb => { listBox2.Items.Add(name);});
}
}
public static class Extensions
{
public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : ISynchronizeInvoke
{
if (control.InvokeRequired)
{
control.Invoke(new Action(() => action(control)), null);
}
else
{
action(control);
}
}
}
For Func, a simple example, here we pass in an instance of StudentGrade and return a DTO, StudentEntity, pass in one type below and get another type back,
public partial class StudentGrade
{
public static Expression<Func<StudentGrade, StudentEntity>> Projection
{
get
{
return (student) => new StudentEntity()
{
PersonID = student.StudentID,
CourseID = student.CourseID,
FirstName = student.Student.FirstName,
LastName = student.Student.LastName,
Grade = student.Grade
};
}
}
}
All of these Task, Thread, Events, Action, Func, Pred, Delgates are best understood by reading the docs, finding examples and inspecting them. If I were to answer each one in depth this is not the place to do it and can be ad nauseam.