Understanding the 'using' statement in C#

Understanding the 'using' statement in C#
By
TiNgZ aBrAhAm

Understanding the 'using' statement in C# 

Introduction

This article is an introduction to the using statement in c# and also provides some insight into the actual implementation of the statement.

The Code

When you are using an object that encapsulates any resource, you have to make sure that when you are done with the object, the object's Dispose method is called. This can be done more easily using the using statement in C#. The using statement simplifies the code that you have to write to create and then finally clean up the object. The using statement obtains the resource specified, executes the statements and finally calls the Dispose method of the object to clean up the object. The following piece of code illustrates its use.

using (TextWriter w = File.CreateText("log.txt"))
{
    w.WriteLine("This is line one");
}

Now that's cool. But before we can start using this code, let us try to understand what happens behind the screen. Lets have a look at the IL code for the above code section.

.locals init ([0] class [mscorlib]System.IO.TextWriter w)
  IL_0000:  ldstr      "log.txt"
  IL_0005:  call       class [mscorlib]System.IO.StreamWriter 
      [mscorlib]System.IO.File::CreateText(string)
  IL_000a:  stloc.0
 
 .try
  {
    IL_000b:  ldloc.0
    IL_000c:  ldstr      "This is line one"
    IL_0011:  callvirt   instance void [mscorlib]
      System.IO.TextWriter::WriteLine(string)
    IL_0016:  leave.s    IL_0022
  }  // end .try
  finally
  {
    IL_0018:  ldloc.0
    IL_0019:  brfalse.s  IL_0021
    IL_001b:  ldloc.0
    IL_001c:  callvirt   instance void [mscorlib]
      System.IDisposable::Dispose()
    IL_0021:  endfinally
  }  // end handler

Hmmmm.... Well doesn't look like this is my code. That's because I see a try and a finally in the IL code (something that I haven't implemented). Wait a minute. IT IS MY CODE....

Waaaaaah... Somebody changed my code...

Well the truth is, somebody did change your code. The CLR. The CLR converts your code into MSIL. And the using statement gets translated into a try and finally block. This is how the using statement is represented in IL. A using statement is translated into three parts: acquisition, usage, and disposal. The resource is first acquired, then the usage is enclosed in a try statement with a finally clause. The object then gets disposed in the finally clause. For example the following lines of code using the using statement,

 

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

}

gets translated to,

    
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();
}

Hmmm... That explains it.

The above code that uses the using statement corresponds to one of the two possible expansions. When MyResource is a value type, the expansion in the finally block will be

finally
{
((IDisposable)myRes).Dispose();
}

If MyResource is of reference type, the expansion becomes

finally
{
if(myRes != null)
((IDisposable)myRes).Dispose();
}

This way, if a null resource is acquired, then no call will be made to Dispose, thus avoiding any exception that occurs.
Well, that explains everything.

Using 'using'

A typical scenario where we could use the using statement is :

string connString = "Data Source=localhost;Integrated " + 
  "Security=SSPI;Initial Catalog=Northwind;";

using (SqlConnection conn = new SqlConnection(connString))
{
  SqlCommand cmd = conn.CreateCommand();
  cmd.CommandText = "SELECT ID, Name FROM Customers";
  
  conn.Open();

  using (SqlDataReader dr = cmd.ExecuteReader())
  {
    while (dr.Read())
      Console.WriteLine("{0}/t{1}", dr.GetString(0), dr.GetString(1));
  }
}

Note

The using statement is only useful for objects with a lifetime that does not extend beyond the method in which the objects are constructed. Remember that the objects you instantiate must implement the System.IDisposable interface.

There is no equivalent for the using statement in vb.net. You have to use the try finally block.

Details at http://www.codeproject.com/csharp/TinguUsingStatement.asp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在SPARC体系结构中,堆栈(stack)和寄存器(register)是重要的组成部分。 首先,堆栈是一种数据结构,用来存储程序在执行过程中的局部变量、函数调用的返回地址以及调用函数之间的参数。堆栈按照后进先出(Last-In-First-Out, LIFO)的原则工作,即最后进入堆栈的数据最先出栈。在SPARC架构中,堆栈的操作主要通过两个寄存器实现:栈指针寄存器(stack pointer register, SP)和帧指针寄存器(frame pointer register, FP)。栈指针寄存器指向堆栈的顶部,即最新入栈的数据;而帧指针寄存器用于定位函数调用的堆栈帧,即每个函数在堆栈中的存储单元。 其次,寄存器在SPARC架构中被广泛运用,包括用于存储中间结果、操作数以及控制流信息。SPARC架构中有通用寄存器(general-purpose registers, GPR)、浮点寄存器(floating-point registers, FPR)和控制寄存器(control registers)等类型的寄存器。通用寄存器用于存储整数类型的数据,浮点寄存器用于存储浮点数类型的数据,而控制寄存器则用于管理指令流程。 SPARC体系结构中的寄存器主要用于提高数据访问速度和执行指令效率。通过将数据存储在寄存器中,这些数据可以更快速地被指令访问和处理,从而提高程序的执行效率。同时,SPARC还通过寄存器窗口(register windows)技术来提高函数调用的效率。寄存器窗口允许在函数调用时保存和恢复寄存器的状态,减少了数据的堆栈访问,并且使得函数调用的开销更小。 综上所述,了解和掌握SPARC体系结构中的堆栈和寄存器是在进行SPARC架构编程和优化时至关重要的。堆栈提供了一种方便的方式来管理函数调用和局部变量,而寄存器则可以提高程序执行效率和指令访问速度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值