Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: using block: object re-initialization

Tags:

c#

using

Re-initialization within "using" block is a bad idea, to be avoided at all times. Still i am going to ask this:

Why does "using" call dispose on the original value and not on the last reference or re-initialization (which happens if try finally block is used)

MyClass b = new MyClass();// implements Idisposable
MyClass c = new MyClass();
MyClass a ; 

 using (a = new MyClass())
 {
                a = b;
                a = c;
 }

In the above code dispose will be called on original reference and not the newer referenced. This can be easily verified by printing something on console in the dispose method.

However with try{} finally code the last reference dispose method is called.

try
{
   a = new MyClass();
   a = b;
   a = c;
 }
  finally 
   {
   a.Dispose();
  }

MSDN : The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object.

using (Font font1 = new Font("Arial", 10.0f)) 
{
    byte charset = font1.GdiCharSet;
}

Basically "using" translates to:

{
  Font font1 = new Font("Arial", 10.0f);
  try
  {
    byte charset = font1.GdiCharSet;
  }
  finally
  {
    if (font1 != null)
      ((IDisposable)font1).Dispose();
  }
}
like image 801
PRR Avatar asked Feb 22 '10 12:02

PRR


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

There are two forms of using statements defined in the C# specification:

using-statement:
    using   (    resource-acquisition   )    embedded-statement
resource-acquisition:
    local-variable-declaration
    expression

If you have a local-variable-declaration, there wouldn't be any questions. The variable will be read-only in the using block and you can't change it at all. The spec says:

8.13 The using statement

[...] In either expansion, the resource variable is read-only in the embedded statement.

Here, we're dealing with the second form: where resource-acquisition is expression and not a local-variable-declaration. In which case, the C# spec clearly says:

A using statement of the form

 using (expression) statement

has the same two possible expansions, but in this case ResourceType is implicitly the compile-time type of the expression, and the resource variable is inaccessible in, and invisible to, the embedded statement. [emphasis mine]

Obviously, you can't change an invisible, inaccessible variable. Its value is assigned only in the using resource-acquisition clause. Therefore, it'll have the old value of the variable, not the new one.

When you are dealing with an assignment to an already declared variable, you are using this form of the using statement. The fact that you are assigning a value to a variable like

using ( x = something )

is irrelevant. The whole x = something is treated as an expression and only the value of that expression is what matters. It's important to know that "resource variable" is not "x" here. It's an invisible variable. From the compiler's perspective, there's not much difference between the following constructs:

using ( something ) 
using ( x = something )
using ( y = x = something )

In all cases, the expression will get executed and the value is what will get guaranteed disposal, not the variable. What would the compiler supposed to do if this wasn't the defined behavior and you had written the third line in the above block? Dispose x? y? both? neither? The current behavior makes sense.

like image 131
mmx Avatar answered Nov 14 '22 05:11

mmx