如果,否则如果变量没有更新

agr*_*ton -3 c# if-statement

我有这种情况,在if和else if块中声明了相同的变量.问题是绕过if语句,执行移动到else if语句,然后不允许重新分配变量.这是一个简单的例子.

 if (filename.ToLower().Contains(".tif"))
 {
     int test = 0;
     string test1;
 }
 else if (ValidExtension(filename.ToLower()))
 {
     int test = 1; 
     //test still equals 0? Not possible right?
     string test1 = "hello";
     //test1 still equals null? Again not possible..?
     //EDIT:
     //Code that accesses test1 here, but test1 = null unless renamed to anything but test1...
 }
Run Code Online (Sandbox Code Playgroud)

ValidExtension只是检查"filename"的扩展名是否是".tif"的任何其他图像格式.它返回true或false.如果重要,则此代码在GUI线程以外的其他线程上执行.

任何人都可以解释为什么或如何发生这种情况的可能原因?

编辑:

我完全理解这些是范围变量我很清楚微软的文档.在整个编码生涯中,我已经做了很多次,并且工作正常.我只是有一个奇怪的情况,出于某种原因,如果我在它下面的一些代码中使用字符串test1 ="hello",仍然在Else If块中,它返回为null.我对Microsoft文档的理解是test1 ="hello"下的行,test1应该总是="hello"但是它没有,它仍然等于null.但是,如果我将代码更改为test2 ="hello",那么它实际上是"你好".我的问题可能措辞不多,显然不是很清楚.这不是一个菜鸟问题,我想知道这会是什么样的错误?

编辑:

这是正在运行的实际代码..希望这将为问题提供更好的背景.

if (filename.ToLower().Contains(".tif"))
{
    using (BaseBL.BeginImpersonate())
    {
        string output = Path.Combine(BLSettings.ClaimFilesPendingIndexingFolder, string.Format("{0}¡{1}¡{2}", "FileDrop", BaseBL.ApplicationUser, filename));
        var cpyCount = 1;
        while (File.Exists(output))
        {
            output = Path.Combine(BLSettings.ClaimFilesPendingIndexingFolder,
                                                      string.Format("{0}¡{1}¡{2}{3}.tif", "FileDrop", BaseBL.ApplicationUser, filename.Replace(".tif", string.Empty),
                                                                    cpyCount));
            cpyCount++;
        }
        var f = File.Create(output);
        f.Write(file.ToArray(), 0, int.Parse(file.Length.ToString()));
        f.Close();
        f.Dispose();
    }
}
else if (ValidExtension(filename.ToLower()))
{
    var image = new Bitmap(file);
    string output = Path.Combine(BLSettings.ClaimFilesPendingIndexingFolder, string.Format("{0}¡{1}¡{2}.tif", "FileDrop", BaseBL.ApplicationUser, Path.GetFileNameWithoutExtension(filename)));
    //output = null always. If changed to output1 it contains the valid filepath...??
    var cpyCount = 1;
    while (File.Exists(output))
    {
        output = Path.Combine(BLSettings.ClaimFilesPendingIndexingFolder,
                                                  string.Format("{0}¡{1}¡{2}{3}.tif", "FileDrop", BaseBL.ApplicationUser, Path.GetFileNameWithoutExtension(filename),
                                                                cpyCount));
        cpyCount++;
    }
    using (BaseBL.BeginImpersonate())
        image.Save(output, ImageFormat.Tiff);
    image.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

D S*_*ley 7

我有这种情况,在if和else if块中声明了相同的变量.

这是不可能的 - 这是两个不同的变量,在不同的范围内具有相同的名称.我怀疑你如何观察这些变量是不合适的.

要成为相同的变量,需要在以下声明之外声明if:

int test;
string test1;
if (filename.ToLower().Contains(".tif"))
{
    test = 0;
}
else if (ValidExtension(filename.ToLower()))
{
    test = 1; 
    test1 = "hello";
}
Run Code Online (Sandbox Code Playgroud)