Does C # have a way to simulate software transactional memory on a small scale?

Does C # have the ability to temporarily change the value of a variable in a specific area and automatically return it back to the end of the area / block?

For example (not real code):

bool UpdateOnInput = true;

using (UpdateOnInput = false)
{
    //Doing my changes without notifying anyone
    Console.WriteLine (UpdateOnInput) // prints false
}
//UpdateOnInput is true again.

EDIT:

The reason I want the above is because I don't want to do this:

UpdateOnInput = false

//Doing my changes without notifying anyone
Console.WriteLine (UpdateOnInput) // prints false

UpdateOnInput = true
+5
source share
8 answers

No, there is no way to do this directly. There are several different schools of thought on how to do this. Compare and compare these two:

originalState = GetState();
SetState(newState);
DoSomething();
SetState(originalState);

vs

originalState = GetState();
SetState(newState);
try
{
    DoSomething();
}
finally
{
    SetState(originalState);
}

Many will tell you that the latter is “safer.”

This is not necessarily the case.

, , , DoSomething() . , , ? ? , , - . ; , . , , , DoSomething, , - .

, - , , ?

, . , , , , , .

, , . , , , . # " ". , , , .

-. :

void M(Func<int, int> f) {}
void M(Func<string, int> f) {}
...
M(x=>x.Length);

, ,

M((int x)=>{return x.Length;});

M((string x)=>{return x.Length;});

, , , . , , . ? , !

, , , - , : " , ". , .

, , , , . , .

. , , , ? , , , .

(, , . - lambda, ! , , " ".)

+13

, :

bool UpdateOnTrue = true;

// ....

bool temp = UpdateOnTrue;
try
{
    UpdateOnTrue = false;
    //  do stuff
}
finally
{
    UpdateOnTrue = temp; 
}
+12

Try:

public void WithAssignment<T>(ref T var, T val, Action action)
{
    T original = var;
    var = val;
    try
    {
        action();
    }
    finally
    {
        var = original;
    }
}

:

bool flag = false;

WithAssignment(ref flag, true, () =>
{
    // flag is true during this block
});

// flag is false again
+6

, try/finally. , IDisposable, - -, , try/finally ( using).

+5

,

Stack<bool>
+4

... . , bool. , - :-) - Stack:

1) Push the old value onto the stack

2) Download the new value

3) do things

4) Pop from the stack and replace the used value.

Rough example (AKA Untested) (can't find stack syntax right now)

bool CurrentValue = true;
Stack<bool> Storage= new Stack<bool>

Storage.Push(CurrentValue);
CurrentValue=false;
DoStuff();
CurrentValue = Storage.Pop();  
//Continue  
+1
source

You must reorganize your code to use a separate function, for example:

bool b = GetSomeValue();
DoSomething(ModifyValue(b));
//b still has the original value. 

To do this, for the reference type, you need to copy it before messing with it:

ICloneable obj = GetSomeValue();
DoSomething(ModifyValue(obj.Clone()));
//obj still has the original value. 

It is difficult to write the correct code when the values ​​of the variables change a lot. Strive to have as few remaps as possible in your code.

+1
source

All Articles