I would like to write my own custom compound statements that have a similar mechanism to using
and lock
mechanisms where they have code injected at the beginning and end of the statement block before compilation.
I have tried searching for questions who may have asked similar questions but I could not properly figure out what this kind of "code scoping" is called, other than the documentation saying these are compound statements.
Im aware that "lock" and "using" are keywords. I dont want to have my own keywords as I know this is not possible.
Im not sure if this is possible in c# for example:
Rather than doing:
StartContext(8);
//make method calls
EndContext();
This could be reduced down to:
DoSomethingInContext(8) {
//make method calls
}
Ofcourse this does not have to be just a single one line call. The start and ends of encapuslated code could be multiple lines of code thats is inserted by the custom compound statement.
A compound statement is a sequence of zero or more statements enclosed within curly braces. Compound statements are frequently used in selection and loop statements. They enable you to write loop bodies that are more than one statement long, among other things. A compound statement is sometimes called a block.
The compound statements are classified based on the connectives used across the compound statements. The connectives of 'or', 'and', 'if then', 'if and only if', are used to form disjunction statements, conjunction statements, conditional statements, and biconditional statements.
A combination of two or more simple statements is a compound statement. Washington, DC, is the capital of United States and it is snowing. Washington, DC, is the capital of United States or it is snowing. It is not snowing.
Explanation: A compound statement is of the form begin . . . end, and it may contain multiple SQL statements between the begin and the end. A compound statement of the form begin atomic . . . end ensures that all the statements contained within it are executed as a single transaction.
You can rewrite your code just slightly:
DoSomethingInContext(8, () => {
// make method calls
});
The signature for your method would be something like this:
public void DoSomethingInContext(int contextId, Action contextBoundAction)
{
// start/open/enter context
try
{
contextBoundAction();
}
finally
{
// stop/close/exit context
}
}
One thing to be aware of, since there is an alternative answer here that uses IDisposable
, is that this delegate-based syntax can make intellisense in various (older) versions of Visual Studio and ReSharper get a little wonky. Sometimes it tries to help you fill out the parameter to DoSomethingInContext
when you really want it to help you fill out parameters in method calls inside the delegate. This is also true for other IDEs such as older Xamarin Studios, which had severe performance problems regarding nested delegates (if you start nesting these context-bound things).
I wouldn't change my programming style because of this, but be aware of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With