Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

Useful wrapper class to override the Object.ToString method

, 4 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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...

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 Person() { Name = "testName" };
    Console.WriteLine(person.ToString(true));
}

It seems to be a little strange to use the parameter bool showName here, but sometimes it can be useful if you need to print something with options.

P.S.: I don't know whether it works or not in a sealed class. You can try it. Wink | ;)

Sorry for my poor English.

License

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

Share

About the Author


Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 4 Oct 2011
Article Copyright 2011 by smilewei
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid