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, ! , , " ".)