C# Combine methods by passing in field type? -


i wasn't sure how title question correctly here i'm trying do.

say have class customer has id, firstname, , lastname field. have list of customers , want write 1 method write id, firstname, or lastname console depending on 1 specify.

in essense, write 1 method accepts field print out instead of writing 3 seperate methods print out each field type.

i know have read how in c# on past few days brain on overload , slipping mind....

any appreciated.

public void printcustomer<t>(customer c, func<customer, t> func) {    console.writeline("{0} , {1}", c.id, func(c)); } 

usage:

printcustomer(mycustomer, c => c.firstname); 

or

printcustomer(mycustomer, c => c.lastname); 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -