I have a bunch of methods that do the following:
using(var cn = new Connection())
{
//custom code
}
Is there a way I can still use the using
, but just call my custom code?
I have a bunch of methods that do the following:
using(var cn = new Connection())
{
//custom code
}
Is there a way I can still use the using
, but just call my custom code?
What about creating a new method encapsulating your using-block
and putting an action-delegate in?
static void Main(string[] args)
{
doCustomCode(() =>
{
Console.WriteLine("customCode #1");
});
doCustomCode(() =>
{
Console.WriteLine("customCode #2");
});
}
private static void doCustomCode(Action action)
{
using (var con = new Connection())
{
action();
}
}
In case you need somethig more specific than a simple action, just modifiy the according parameter.
You can use any of the following:
try { } finally { }