Home > General > Forcing classes to implement ToString()

Forcing classes to implement ToString()

There are situations where it would be useful to force a class that implements an interface to override the ToString() method.

public interface ITask
{
	void Execute();
	string ToString();
}

In this case, I can execute a list of tasks and log any exceptions that occur. The ToString() method can provide a way to log information about the state or progress of a task when an exception occurs. The exception and stack trace may not be able to provide enough information in all situations.

foreach (ITask task in tasks)
{
	try
	{
		task.Execute();
	}
	catch (Exception e)
	{
		Log.Error("Error occurred: " + task.ToString(), e);
	}
}

Unfortunately, there isn’t a way to force a class to override the ToString() method, even if the method signature is present on the interface. Instead, we can use abstract classes to enforce this by using the abstract and override modifiers together:

public abstract class Task
{
	public abstract void Execute();
	public abstract override string ToString();
}

Any classes that derive from Task will be required to override the ToString() method.

  1. July 23, 2014 at 10:54 pm

    If yyou want a beaded Rasta bracelet or reggae bracelets,
    there are numerous crraft stores and outlets that offer different kinds of beads
    that vary in sizes and shapes. anyone could
    freely pass through its dozen or so wide gates. Long ago
    the Natove American Indian decorated their garments wiith painted designs.

  2. August 8, 2014 at 6:46 pm

    Thanks for finally writing about >Forcing classes to implement ToString() | Malvin Ly <Loved it!

  3. August 9, 2014 at 6:00 am

    Thanks for finally writing about >Forcing classes to implement ToString() | Malvin Ly <Loved it!

  4. August 11, 2014 at 5:29 am

    Attractive sectikn of content. I just stumbled upon your
    blog and in accession capital to assert that I acquire in fact enjoyed account your blog posts.
    Anyway I’ll be subscribing to your augment and even I achievement you access consistently rapidly.

  5. August 16, 2014 at 4:49 am

    It’s a pity you don’t have a donate button!
    I’d without a doubt donate to this superb blog! I guess for now i’ll
    settle for bookmarking and adding your RSS
    feed to my Google account. I look forward to fresh updates and will share this blog with my
    Facebook group. Talk soon!

  6. August 23, 2014 at 7:30 am

    Once you do have a solid business idea, it’s time to generate a small business plan. This will be the primary reason why every site
    owner should have a fantastic web hosting plan. Parenting, I believe will
    be the most noble occupation of – and parents want their children safe on the internet, so.

  7. August 26, 2014 at 9:45 pm

    An impressive share! I have just forwarded this onto a co-worker who had
    been conducting a little homework on this. And he in fact bought me dinner because I found
    it for him… lol. So allow me to reword this…. Thank YOU for
    the meal!! But yeah, thanx for spending time
    to talk about this matter here on your internet site.

  8. August 27, 2014 at 3:06 pm

    I was curious if you ever considered changing thhe page layout of yohr website?
    Its very well written; I love what youve got to say. But maybe yyou could
    a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text forr only having 1 or two images.

    Maybe you could space it out better?

  9. August 27, 2014 at 4:59 pm

    I’m noot sujre exactly why but this web site is loading very slow ffor me.
    Is anyone else having this issue or is it a issue on my
    end? I’ll check back later on and see if the problem still exists.

  10. August 29, 2014 at 9:39 am

    Hey There. I discovered your weblog the usage of msn. This is
    a very well written article. I’ll be sure to bookmark it and return to learn more of your useful info.
    Thank you for the post. I will certainly return.

  11. August 29, 2014 at 7:17 pm

    Thankfulness tto my father who stated to
    me about this website, this weblog is truly awesome.

  12. August 30, 2014 at 1:08 am

    obviously like youjr web-site however you have tto take a look at the
    spelling on quite a few of your posts. Many of them are rife wth
    spelling issues andd I to find it very bothersome
    to infprm the trruth on thee other hand I will surely
    come again again.

  13. September 4, 2014 at 11:57 pm

    Thanks , I’ve just been looking for information approximately this topic for a while and yours is the best I’ve found out so far.

    But, what concerning the bottom line? Are you certain concerning thee supply?

  14. September 5, 2014 at 5:33 pm

    He was a primary factor at the time in the huge gain in popularity of Martial
    Arts in America and the World, but he was not the reason for all the
    events discussed in the show. In the meantime, if not saved by
    private means, abandoned and abused dogs and cats will
    continue to be exterminated like trash in Mobile County
    Animal Control, as well as in most public animal facilities throughout the state.
    Further, in multiple weapon martial arts such as Okinawan kobudo,
    distances, timing, techniques, etc.

  15. September 6, 2014 at 12:47 am

    Hi there would you mind letting me know which web host you’re utilizing?I’ve loaded yoour blog in 3 different
    internet browsers and I must say thus blog loads a lott faster then most.
    Can you suggest a good web hosting prrovider at a fair price?
    Thanks, I appreciate it!

  16. September 8, 2014 at 12:49 am

    Hi there, I found your blog by way of Gogle while searching for a comparable topic, our website got here up,
    it seems good. I have bookmarked it in my google
    bookmarks.
    Hello there, simply become aware of your weblog thru Google,
    and located that it is really informative. I’m going to watch ouut for brussels.
    I will appreciate when youu proceed this in future. Maany other people might be benefited out of your writing.

    Cheers!

  17. September 23, 2014 at 2:55 am

    Wow, incredible weblog layout! How lengthy have you been running a blog for?
    you make blogging look easy. The total look of your web site is fantastic, as neatly as the content material!

  18. September 25, 2014 at 7:43 am

    The problem with so many plans for attaining Internet marketing success is that there are many
    forms of marketing that just about everyone else has been using.

    Test it, change it and see what works try to make one dollar from
    doing this one thing. Many small businesses and service providers naturally hope to take their products and services to a worldwide marketplace via Internet marketing.

  19. October 2, 2014 at 1:42 am

    fantastic points altogether, you simply gained a new reader.
    What would yoou suggest in regards to your submit that you just made
    a few days ago? Any certain?

  20. October 3, 2014 at 4:59 pm

    Howdy! I know this is somewhat off topic but I was wondering
    if you knew where I could get a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

  21. October 6, 2014 at 4:09 pm

    fantastic points altogether, you just won a new reader.
    What may you recommend about your post that you made a few days ago?

    Any positive?

  22. October 13, 2014 at 2:37 am

    I do trust all of thee concepts you’ve offered in your post.

    They’re really convincing and will definitely work. Still, the posts are very brief for beginners.

    Could you please extend them a little from subsequent time?
    Thank you for the post.

  23. November 14, 2014 at 6:32 pm

    Wow, that’s what I was searching for, what a information! existing here at this
    webpage, thanks admin of this website.

  24. December 23, 2014 at 2:56 am

    I rarely leave a response, however i did some searching and wound up
    here Forcing classes to implement ToString() | Malvin Ly.
    And I actually do have 2 questions for you if it’s allright.
    Is it just me or does it look like a few of these
    comments come across like written by brain dead individuals?
    😛 And, if you are writing on additional online social sites, I’d like to follow anything
    fresh you have to post. Would you list of
    all of all your social pages like your Facebook page,
    twitter feed, or linkedin profile?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: