C# Wishlist: Post-Constructor Method

I would love it if C# had a predefined method that would be guaranteed to be called after every constructor is done firing. I have some checking code that I need to call, but depending on which constructors are called, it could need to be fired in a few different places. If I had a PostConstructor() method or something, I could centralize the checking code there.

It would also be cool if you could provide a method that would be called before or after every method call, like the SetUp() and TearDown() special methods in NUnit, but without all the reflection overhead at runtime.


3 Responses to “C# Wishlist: Post-Constructor Method”

  1. 1 W. Kevin Hazzard January 31, 2008 at 12:00 pm

    You could, of course, place the construction in a try block and in the finally block, do your post construction proessing. The finally block will alsways run. — Kevin

  2. 2 Anthony Stevens January 31, 2008 at 12:04 pm

    Hm … wouldn’t I have to put a try/finally block in every constructor? That’s what I would like to avoid. Or are you thinking that the callers would new() in a try/finally block?

  3. 3 Raja Nagendra Kumar October 1, 2008 at 3:23 am

    We too are looking for such feature, in another context..

    It is in the inheritance chain, few methods needs to be call after both parent and child constructors are called.. such as display of the UI screen..

    Showing it in parent would mean, before child adds all its specialisations, the form would get displayed etc..

    I would see this to be OOAD extention feature..

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

TwitterCounter for @anthonyrstevens
Add to Technorati Favorites

RSS Feed

View Anthony Stevens's profile on LinkedIn

%d bloggers like this: