Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果出现故障,你会关闭连接吗?_C#_Asp.net_.net_Using - Fatal编程技术网

C# 如果出现故障,你会关闭连接吗?

C# 如果出现故障,你会关闭连接吗?,c#,asp.net,.net,using,C#,Asp.net,.net,Using,如果我在try catch中使用using(关闭所有连接),我很感兴趣 try{ using (var browser = new IE("https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html")) { clsUtils.WriteToLog("Trying to login", true, true);

如果我在try catch中使用using(关闭所有连接),我很感兴趣

try{
     using (var browser = new IE("https://www.bbvanetcash.com/local_kyop/KYOPSolicitarCredenciales.html"))
                    {

                        clsUtils.WriteToLog("Trying to login", true, true);
                        browser.Visible = false;
                        browser.TextField(Find.ByName("cod_emp")).Value = _company;
                        browser.TextField(Find.ByName("cod_usu")).Value = _strUser;
                        browser.TextField(Find.ByName("eai_password")).Value = _strPass;
                        browser.Button(Find.ByClass("grandote estirado azul")).Click();
                        browser.WaitForComplete();
                        clsUtils.WriteToLog("Logged ", true, true);
                        connected = true;
                        var cookie = browser.Eval("document.cookie");
                        CookieContainer Cc = GetCc(cookie);


    } catch (Exception ex)
            {
                Console.WriteLine("(TryToLogin)-->Catching the {0} exception .", ex.GetType());
                return connected;
            }
}
如果其中一个步骤失败,用户将关闭连接,或者它只是去捕捉?

:

using
语句确保调用
Dispose
,即使在对对象调用方法时发生异常。通过将对象放入
try
块中,然后在
finally
块中调用
Dispose
,可以获得相同的结果;事实上,编译器就是这样翻译
using
语句的


是的,如果您使用
使用
语句,则必须实现Idispose类

所以,如果你正在使用下面的东西

using (foo)
{
    //...
}
编译器将解释如下

try
{
    //...
}
finally
{
    foo.Dispose();
}
希望这有帮助