A little C# inspired by Ruby... - No Fluff Just Stuff

A little C# inspired by Ruby...

Posted by: Venkat Subramaniam on March 8, 2007

I've been wanting to blog this for months, but could not find a few minutes to sit down for it.
Technology is great. I am in a car (relax, no, I'm not driving at the moment!) and decided this
is a great time to put that wireless network to use and blog this finally.

Inspired by Ruby and Anonymous delegates in C#, in some special cases, you can manage proper
resource clean up, and/or, execution of pre and post operations. We know that there is no guarantee
when finalize is called and there is no way to insist that our users call Dispose. What if calling Dispose
is very critical or if you have to do some post-processing in your code. Well, here is a way to do just that.

using System;
using System.Collections.Generic;
using System.Text;

namespace ResourceCleanExample
{
    public delegate void ResourceUseHelper(Resource resource);

    public class Resource : IDisposable
    {
        private Resource() { Console.WriteLine("Pre-procesing is taken care..."); }

        public static void UseResource(ResourceUseHelper resourceHelper)
        {
            Resource resource = new Resource();

            try
            {
                resourceHelper(resource);
            }
            finally
            {
                resource.Dispose();
            }
        }

        #region IDisposable Members

        public void Dispose() // In addition follow the Dispose Design Pattern...
        {
            Console.WriteLine("Any cleanup or post-processing is taken care...");
        }

        public void someOp1() { Console.WriteLine("Some op1 done..."); }
        public void someOp2() { Console.WriteLine("Some op2 done..."); }

        #endregion
    }

    public class Program
    {
        static void Main(string[] args)
        {
            Resource.UseResource(delegate(Resource r)
            {
                r.someOp1();
                r.someOp2();
            }
            );
        }
    }
}

The output from above code is:

Pre-procesing is taken care...
Some op1 done...
Some op2 done...
Any cleanup or post-processing is taken care...
Venkat Subramaniam

About Venkat Subramaniam

Dr. Venkat Subramaniam is an award-winning author, founder of Agile Developer, Inc., creator of agilelearner.com, and an instructional professor at the University of Houston.

He has trained and mentored thousands of software developers in the US, Canada, Europe, and Asia, and is a regularly-invited speaker at several international conferences. Venkat helps his clients effectively apply and succeed with sustainable agile practices on their software projects.

Venkat is a (co)author of multiple technical books, including the 2007 Jolt Productivity award winning book Practices of an Agile Developer. You can find a list of his books at agiledeveloper.com. You can reach him by email at venkats@agiledeveloper.com or on twitter at @venkat_s.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »