Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / C#

Useful wrapper class to override the Object.ToString method

Rate me:
Please Sign up or sign in to vote.
4.64/5 (9 votes)
3 Oct 2011CPOL 37.8K   9  
A useful wrapper class to override the Object.ToString method.

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
4 Oct 2011kct
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));
Please Sign up or sign in to vote.
4 Oct 2011smilewei
You can write an extension method like this:public static class ToStringExtensionMethod{ public static string ToString(this Person person, bool showName) { return person.Name; }}then you can use it like this:public class Program{ Person person = new...

License

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


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

Comments and Discussions