Click here to Skip to main content
15,887,477 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Useful wrapper class to override the Object.ToString method

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Oct 2011CPOL 3.7K  
Here with generics:public static string ToString(this T obj, Func toStringFunction) { return toStringFunction(obj); }You can use it like this:Console.WriteLine(person.ToString(p=>p.Name));
Here with generics:
C#
public static string ToString<T>(this T obj, Func<T, string> toStringFunction)
     {
         return toStringFunction(obj);
     }

You can use it like this:
C#
Console.WriteLine(person.ToString(p=>p.Name));

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
kct
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --