Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Variable does not exists in current context

I have a problem I can't seem to solve.

I created a class function called test and in the function I declared a variable. On the next line I fill the function with a string.

During debugging the variable does not get declared, my variable watcher in VS tells me that the variable does not exists in the current context.

Can you all help me solve this problem ?

Here is my code:

public void Test()
{
    string DirectoryPath;
    DirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache);
}
like image 657
P.G Wisgerhof Avatar asked Aug 21 '26 13:08

P.G Wisgerhof


1 Answers

My guess is you're using a Release configuration - the optimizer may have removed the variable, as it's pointless other than for debugging. You assign it a value, but never read it. In a Debug configuration, I'd expect it to be fine (but possibly create a warning).

EDIT: Of course, this is assuming that you were in the Test() method that you couldn't see the variable. If Test() had already completed then Likurg's answer is probably more appropriate.

like image 169
Jon Skeet Avatar answered Aug 23 '26 04:08

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!