Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Compound statements in c#

Tags:

c#

statements

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.

like image 251
Gelion Avatar asked Feb 19 '18 09:02

Gelion


People also ask

What are the compound statements in C?

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.

What are the 5 main kinds of compound statement?

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.

What is compound statement with example?

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.

What is the format for compound statement?

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.


1 Answers

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.

like image 61
Lasse V. Karlsen Avatar answered Sep 30 '22 12:09

Lasse V. Karlsen