×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Need clarification for using {} statement in C#

Need clarification for using {} statement in C#

Need clarification for using {} statement in C#

(OP)
Colleagues,

the C# Reference on the statement in the subject line states: "Provides a convenient syntax that ensures the correct use of IDisposable objects."
The rest is even less comprehensible for yours truly.
The only thing I could derive from that article

https://docs.microsoft.com/en-us/dotnet/csharp/lan...

was that, whatever memvars were declared within this using {} block, will be properly and safely disposed of (something like free after malloc in C).

Did I get it correctly?

If so - Is it the only useful thing about using {} block?

If not - what else is there that might be useful?

AHWBGA!

Regards,

Ilya

RE: Need clarification for using {} statement in C#

Yes, the variable declared in the using statement will automatically be disposed when the using clause ends. I use it a lot when I'm working with the SAP BusinessObjects .NET SDK, part of which is still built on COM objects. I write wrapper classes that include a disposer so that I can make sure that the memory used by the COM object is immediately freed instead of waiting for garbage collection, which can take a while for COM objects.

-Dell

Senior Manager, Data & Analytics
Protiviti
www.protiviti.com

RE: Need clarification for using {} statement in C#

(OP)
Got it, thank you!

Any other reasons to implement this technique?

Regards,

Ilya

RE: Need clarification for using {} statement in C#

Like C {} they also go out of scope which is really useful when you write experimental code and cut and paste various bits.

RE: Need clarification for using {} statement in C#

(OP)
OK, got it, thank you, xwb!
Still the following is not exactly clear for this old "newbie" ( smile ): "variable(s) disposed off" - is the memory (heap, stack, or both) occupied by declared within the using {} block memvars cleaned out upon exiting out of this block? E.g. if I declared a memvar as a pointer on an object of some class, will it clean out from memory not only the pointer but the object as well?
Or I'd rather run GC.Collect() method within this using{} block myself as the last command in it? (BTW: GC.Collect() w/o arguments - does it clean-up all memvars including public ones? Or just those within the scope of the code block it is issued in?

Regards,

Ilya

RE: Need clarification for using {} statement in C#

There is no need to run GC.Collect() explicitly unless you are working on an embedded application where GC is critical. If you take a reference to a local object inside the using, it will not get cleaned up until the reference is released. Taking a reference to a local object can often cause huge memory leaks so it is not recommended.

The clean up does all variables created within the scope of the using that are no longer in use.

RE: Need clarification for using {} statement in C#

(OP)
Not quite sure I follow...
Let me try another approach. Say, you have this class (honestly stolen from MSDN)

CODE --> c#

public class CustomClass
   {
      public int Number { get; set; }
      
      public int Multiply(int num)
      {  return num * Number; }
      public CustomClass()
      {  Number = 0;}
   }
public void main()
using {
   CustomClass objTest = new CustomClass(10);
   int iRes = objTest.Multiply(5);
   MessageBox.Show(iRes.ToString());
   } 
In the code above, will the memory occupied by objTest, that is CustomClass' instance, be freed along with the objTest pointer (or "reference', if you will) after the closing bracket of the using {} block?
Or I need to do it explicitly by calling in garbage collector?

TIA!

Regards,

Ilya

RE: Need clarification for using {} statement in C#

Both will be freed.

-Dell

Senior Manager, Data & Analytics
Protiviti
www.protiviti.com

RE: Need clarification for using {} statement in C#

(OP)
Got it.
Now (digging even deeper): if it's something like

CODE --> C#

using (Stream result = new FileStream(tempFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
   {
      using (XmlWriter writer = XmlWriter.Create(result, new XmlWriterSettings { CloseOutput = false })) //Line 1
      using (XmlReader reader = XmlReader.Create(inputFilePath))                                         //Line 2
      using (XmlReader helperReader = XmlReader.Create(inputFilePath))                                   //Line 3
      {
         using (XmlReader ptreader = XmlReader.Create(inputFilePath))                                    //Line 4
// some more code w/o using, and then
       }
    } 
when the pointers and objects declared on the lines I've marked "Line1"..."Line4" will be released?
IOW, what's the "signal" for the system to release these thingies? On the closing parenthesis, or at the end of the function (since they're all local), or ... what?

TIA!

Regards,

Ilya

RE: Need clarification for using {} statement in C#

The close bracket that indicates the end of the "using".

-Dell

Senior Manager, Data & Analytics
Protiviti
www.protiviti.com

RE: Need clarification for using {} statement in C#

(OP)
Thus, in the code above, since the memvar "result" is declared inside the "using's" brackets (round ones, a.k.a. "parenthesis"), it is discarded right after its declaration?
ponder
Or you meant "braces" ("curly" brackes)?
Please clarify.
TIA!

Regards,

Ilya

RE: Need clarification for using {} statement in C#

No, it's the curly braces - the round ones are parentheses.

-Dell
Senior Manager, Data & Analytics
Protiviti
www.protiviti.com

RE: Need clarification for using {} statement in C#

(OP)
Ignorant as I am (not!), I happen to know the difference between "parenthesis" and "braces", colleague Dell. winky smile
Seriously now:

In the code above, posted on 29 Jan 20 @ 17:24, there are several "using" statements w/o braces, the contents in parenthesis only (lines 1...3). And my question now is:

At what moment the memvars declared within outermost parentheses in those lines, with no braces, will be discarded from memory?

1. Right at the closing parenthesis?
OR
2. When the code block within braces, of which this brace-less (if that's the word) ends? (That including the starting and ending braces of the function/procedure/method it's in.)

Regards,

Ilya

RE: Need clarification for using {} statement in C#

I always use a set of braces after each using statement because there are multiple lines of code that the using applies to. However, after looking at the MS documentation for using, I'm going to recommend changing your code to this so that all of your XmlReaders are initialized in the same using block:

CODE

using (Stream result = new FileStream(tempFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{     
   using (XmlWriter writer = XmlWriter.Create(result, new XmlWriterSettings { CloseOutput = false })) 
     using (XmlReader reader = XmlReader.Create(inputFilePath),
                      helperReader = XmlReader.Create(inputFilePath),                                   
                      ptreader = XmlReader.Create(inputFilePath))  
     {                                  
        // multiple lines of code
     }
} 

The documentation https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/statements#the-using-statement indicates that the using block is just shorthand for the following logic:

CODE

{
    ResourceType resource = expression;
    try {
        statement;
    }
    finally {
        if (resource != null) ((IDisposable)resource).Dispose();
    }
} 

There are various types of statements that go in the "finally" block depending on whether resource is nullable or dynamic, but the logic is all similar. This means that if there's just a single line of code that the using applies to, you don't need braces and the variable declared in the using will be disposed when the statement completes. If you have multiple lines of code the using applies to, you must use the braces and the variables are disposed when the braces are closed.

-Dell

Senior Manager, Data & Analytics
Protiviti
www.protiviti.com

RE: Need clarification for using {} statement in C#

(OP)
Unfortunately, colleague Dell, this is not mine (but vendor's) program that I need to document... NO-CAN-DO change anything, it's "verbotten". sad

Regards,

Ilya

RE: Need clarification for using {} statement in C#

Following the documentation that I linked to, the three variables inside the outer braces will be disposed when the inner braces are closed.

-Dell

Senior Manager, Data & Analytics
Protiviti
www.protiviti.com

RE: Need clarification for using {} statement in C#

Hi Ilya,

the usage of "using" is similar to that of "if" or "for": if only one statement follows, no need for braces, even if that one statement following is another "using" statement.
Therefore, your code is equivalent to something like this:

CODE --> C#

using (XmlWriter writer = XmlWriter.Create(result, new XmlWriterSettings { CloseOutput = false })) //Line 1
{ // implicit brace 1
      using (XmlReader reader = XmlReader.Create(inputFilePath))                                         //Line 2
	  { // implicit brace 2
		  using (XmlReader helperReader = XmlReader.Create(inputFilePath))                                   //Line 3
		  {
			 using (XmlReader ptreader = XmlReader.Create(inputFilePath))                                    //Line 4
			 // one line statement or another implicit brace?
		   } // end of scope helperReader, object disposed
	   } // end implicit brace 2, reader end of scope, disposed
} // end implicit brace 1, xmlWriter end of scope, disposed, end of scope for result 

No need to change / rewrite any code. What I put above is the exact same as your code, only differently structured so that you can better see the scope of each object and when it is released.

Hope that helps.

Cheers,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Need clarification for using {} statement in C#

(OP)
Thank you, MakeItSo!
That's what I suspected: these "implicit braces" (don't like them even more than implicit memvars' declaration) was what "threw me off the rails" (and "drove me up the wall" - or ceiling, rather).
Case's closed.
My gratitude to everybody who helped me here to understand and resolve this problem! thanks2 bow

Regards,

Ilya

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close