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
Post a Comment