&QUOT的使用;使用"在C#中 [英] Uses of "using" in C#

查看:124
本文介绍了&QUOT的使用;使用"在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

KOKOS 回答了精彩的由提关键字C#问题隐藏的功能。你能详细说说吗?什么是良好的使用

User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are good uses of using?

推荐答案

究其原因,使用的声明是保证,因为它超出范围的对象尽快处理,而且它不需要明确的code到确保这种情况的发生。

The reason for the "using" statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens.

作为每个<一href=\"http://www.$c$cproject.com/KB/cs/tinguusingstatement.aspx\">http://www.$c$cproject.com/KB/cs/tinguusingstatement.aspx,在.NET CLR转换

As per http://www.codeproject.com/KB/cs/tinguusingstatement.aspx, the .NET CLR converts

using (MyResource myRes = new MyResource())
{
    myRes.DoSomething();
}

{ // limits scope of myRes
    MyResource myRes= new MyResource();
    try
    {
        myRes.DoSomething();
    }
    finally
    {
        // Check for a null resource.
        if (myRes!= null)
            // Call the object's Dispose method.
            ((IDisposable)myRes).Dispose();
    }
}

这篇关于&QUOT的使用;使用&QUOT;在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆