C# - Variable does not exists in current context

[亡魂溺海] 提交于 2019-12-04 19:41:35

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.

If i'm not mistake you want to do this

    public class MyTest
    {
        string DirectoryPath = "";
        public void Test()
        {
            DirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache);
        }
        public void UseString()
        {
            //Use DirectoryPath
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!