Passing Statement Block as Parameter?

  • Thread starter Thread starter O.B.
  • Start date Start date
O

O.B.

I have the following code in many places throughout my code.

if (IsValid())
{
try
{
// Code-to-execute
}
finally
{
Cleanup();
}
}

I'd like to replace it with:

NewOperation()
{
// Code-to-execute
}

I believe that this is possible because it appears that C# operations
like "lock" do exactly this. How would one go about creating
NewOperation?
 
I'm not sure what you mean about "operations like 'lock' do exactly  
this".  Either I've misunderstood your question or you've misunderstood 
"lock()".

As far as the part of your question I think I understand goes, you can do 
something like this:

NewOperation(Action action)
{
     if (IsValid())
     {
         try
         {
             action();
         }
         finally
         {
             Cleanup();
         }
     }

}

and then call it like this:

     NewOperation(delegate()
         {
             // code-to-execute
         });

Pete

When invoking lock(), you can invoke it as follows:

lock(someObject)
{
// code-to-execute
}
 
When invoking lock(), you can invoke it as follows:

lock(someObject)
{
  // code-to-execute

}

"lock" is a language keyword, not a library-defined function, same as
"if" and "for". You cannot write something that looks precisely like
it in C#. The closest you can get to that is by using anonymous
delegates / lambdas, which Peter has shown you how to do. The only
thing I can add to that is that you might consider using a shorter
syntax from C# 3.0 for that:

NewOperation(() =>
{
// code-to-execute
});

By the way, by now, it's a common .NET pattern to use lambdas for that
sort of thing. This is the way PLINQ does it, for example:

List<int> list;
...
Parallel.ForEach(list, item => Console.WriteLine(item)); // as close
to "foreach" statement as it can get
 
I'm not sure what you mean about "operations like 'lock' do exactly  
this".  Either I've misunderstood your question or you've misunderstood 
"lock()".

As far as the part of your question I think I understand goes, you can do 
something like this:

NewOperation(Action action)
{
     if (IsValid())
     {
         try
         {
             action();
         }
         finally
         {
             Cleanup();
         }
     }

}

and then call it like this:

     NewOperation(delegate()
         {
             // code-to-execute
         });

Pete

I must be missing something. System.Action requires a template
parameter.
 
I must be missing something.  System.Action requires a template
parameter.

Call it stupidity. I just declared it myself as a delegate.
Everything seems to be working fine. The Lamda expression is a nice
bonus. Thanks for the help, Pete and Pavel!
 
I must be missing something.  System.Action requires a template
parameter.

If that is the case, then you must be using .NET 2.0 libs - that one
only had System.Action<T>. .NET 3.5 also added System.Action,
System.Action<T1, T2>, etc up to 4 args.
 
Call it stupidity. I just declared it myself as a delegate.
Everything seems to be working fine. The Lamda expression is a nice
bonus. Thanks for the help, Pete and Pavel!

It's not stupidity. Action<>, Lambda functions or a custom delegate will all
do what you need in this case. Just use whichever you find easiest to read
and maintain.

Andrew Faust
 
Back
Top