We pay for user submitted tutorials and articles that we publish. Anyone can send in a contributionLearn More
Have you ever tried using the "Using" statement with a method and not a class?
We all have done the following:
Whether you know it or not, the compiler translates that code to the following:
And while doing that makes sure that the Dispose method is called so files and data base connections are closed and so on. Why not take the "Using" statement and make it more useful. The Dispose method we want to be called does not have to Dispose of stuff, it can do anything we want!
Lets see how we can do this. Suppose you have an operation that and we want to know how long it takes. all we have to do is the following:
Look at the Using Statement we are "Using" a Method not a class.
Look at the Counting Class:
As you can see the OperationTimerCounter class returns a IDisposable TimeCounter Class which logs the start time TickCount. when we exit the scope the Dispose method is called and the subtraction is printed to the console. Great isn’t it?
I think it is very useful, although muse be handled with care or it can turn into a mess.
To download a Sample Project Check out the bottom of our RSS feed for the password to the Freebies page
Copyright © 2012 Dev102.com
Breeze : Designed by Amit Raz and Nitzan Kupererd