c#中Using块的目的是什么?它和局部变量有什么不同?

在MySQL JOIN中,ON和USING()之间的区别是什么?据我所知,USING()只是更方便的语法,而ON在列名不相同时允许更大的灵活性。然而,这种差异是如此之小,您可能会认为他们只是取消了USING()。

还有什么比看上去更重要的吗?如果是,在给定的情况下我应该使用哪一种?

用户kokos通过使用关键字回答了c#的隐藏特性问题。你能详细说明一下吗?使用的用途是什么?

我正在做一个项目。我必须比较两个文件的内容,看看它们是否完全匹配。

在进行大量的错误检查和验证之前,我的第一稿是:

  DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory + "\\TestArea\\");
  FileInfo[] files = di.GetFiles(filename + ".*");

  FileInfo outputFile = files.Where(f => f.Extension == ".out").Single<FileInfo>();
  FileInfo expectedFile = files.Where(f => f.Extension == ".exp").Single <FileInfo>();

  using (StreamReader outFile = new StreamReader(outputFile.OpenRead()))
  {
    using (StreamReader expFile = new StreamReader(expectedFile.OpenRead()))
    {
      while (!(outFile.EndOfStream || expFile.EndOfStream))
      {
        if (outFile.ReadLine() != expFile.ReadLine())
        {
          return false;
        }
      }
      return (outFile.EndOfStream && expFile.EndOfStream);
    }
  }

嵌套using语句似乎有点奇怪。

还有更好的办法吗?

system.net.httpclient和system.net.httpclienthandler在。net Framework 4.5中实现了IDisposable(通过System.Net.Http.HttpMessageInvoker)。

using语句文档说:

作为规则,当使用IDisposable对象时,应该声明和 在using语句中实例化它。

这个答案使用了这个模式:

var baseAddress = new Uri("http://example.com");
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("foo", "bar"),
        new KeyValuePair<string, string>("baz", "bazinga"),
    });
    cookieContainer.Add(baseAddress, new Cookie("CookieName", "cookie_value"));
    var result = client.PostAsync("/test", content).Result;
    result.EnsureSuccessStatusCode();
}

但是来自Microsoft的最明显的例子既没有显式调用也没有隐式调用Dispose()。例如:

最初的博客文章宣布HttpClient的发布。 HttpClient的实际MSDN文档。 BingTranslateSample GoogleMapsSample WorldBankSample

在公告的评论中,有人问微软员工:

在检查了你的样品后,我发现你没有进行处理 对HttpClient实例的操作。我已经使用了HttpClient的所有实例 在我的应用程序上使用声明,我认为这是正确的方式 因为HttpClient实现了IDisposable接口。我是在 正确的道路?

他的回答是:

一般来说,这是正确的,尽管你必须小心 "using"和async,因为它们在。net 4和。net 4.5中不能真正混合 可以在“using”语句中使用“await”。 顺便说一句,你可以重复使用同一个HttpClient,次数不限 通常情况下,您不会一直创建/处理它们。

第二段对这个问题来说是多余的,它不关心您可以使用HttpClient实例多少次,而是关心在您不再需要它之后是否有必要处理它。

(更新:事实上,第二段是答案的关键,如下文由@DPeden提供。)

所以我的问题是:

Is it necessary, given the current implementation (.NET Framework 4.5), to call Dispose() on HttpClient and HttpClientHandler instances? Clarification: by "necessary" I mean if there are any negative consequences for not disposing, such as resource leakage or data corruption risks. If it's not necessary, would it be a "good practice" anyway, since they implement IDisposable? If it's necessary (or recommended), is this code mentioned above implementing it safely (for .NET Framework 4.5)? If these classes don't require calling Dispose(), why were they implemented as IDisposable? If they require, or if it's a recommended practice, are the Microsoft examples misleading or unsafe?

我喜欢在using块中实例化我的WCF服务客户端,因为它几乎是使用实现IDisposable的资源的标准方式:

using (var client = new SomeWCFServiceClient()) 
{
    //Do something with the client 
}

但是,正如这篇MSDN文章中提到的,将WCF客户端包装在using块中可能会掩盖导致客户端处于故障状态(如超时或通信问题)的任何错误。长话短说,当调用Dispose()时,客户端的Close()方法会触发,但会抛出一个错误,因为它处于故障状态。然后,原始异常被第二个异常掩盖。不好的。

在MSDN文章中建议的解决方法是完全避免使用using块,而是实例化你的客户端,并像这样使用它们:

try
{
    ...
    client.Close();
}
catch (CommunicationException e)
{
    ...
    client.Abort();
}
catch (TimeoutException e)
{
    ...
    client.Abort();
}
catch (Exception e)
{
    ...
    client.Abort();
    throw;
}

与using块相比,我认为它很难看。每次需要客户端时都要写大量代码。

幸运的是,我找到了一些其他的解决方法,比如IServiceOriented博客上的这个(现在已经不存在了)。你可以这样开始:

public delegate void UseServiceDelegate<T>(T proxy); 

public static class Service<T> 
{ 
    public static ChannelFactory<T> _channelFactory = new ChannelFactory<T>(""); 
    
    public static void Use(UseServiceDelegate<T> codeBlock) 
    { 
        IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel(); 
        bool success = false; 
        try 
        { 
            codeBlock((T)proxy); 
            proxy.Close(); 
            success = true; 
        } 
        finally 
        { 
            if (!success) 
            { 
                proxy.Abort(); 
            } 
        } 
     } 
} 

这就允许:

Service<IOrderService>.Use(orderService => 
{ 
    orderService.PlaceOrder(request); 
}); 

这还不错,但我不认为它像using块那样具有表现力和易于理解。

我目前正在尝试使用的解决方法是我第一次在blog.davidbarret.net上读到的。基本上,无论在哪里使用客户机的Dispose()方法,都要重写它。喜欢的东西:

public partial class SomeWCFServiceClient : IDisposable
{
    void IDisposable.Dispose() 
    {
        if (this.State == CommunicationState.Faulted) 
        {
            this.Abort();
        } 
        else 
        {
            this.Close();
        }
    }
}

这似乎能够允许using块再次出现,而不会有隐藏故障状态异常的危险。

那么,使用这些变通方法还有其他需要注意的问题吗?有没有人想出更好的办法?