locked
Keyword 'using' RRS feed

  • Question

  • I've got some old C# code I'm supposed to update with new some functionallity.

    There are a some things that I haven't seen before and can not find described in my C# books.

    It is the use of keyword 'using', not in the header of the file but directly in the code as well.

    Example:

     try
                    {
                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(errorFolder + @"\" + errorFileName + ".log", true, Encoding.Unicode))
                        {
                            file.Write(timeOut.ToString());
                        }
                    }
                    catch { }

    Since 'using' is a quite common word, it's difficult to search an explanation.

    I do understand what the example code does. It is the reason to construct it with the keyword 'using' I do not understand.

    Wednesday, February 8, 2012 10:36 AM

Answers

  • Hi,

    it's used on objects that implement IDisposable, and it is the equivalent of this:

    try

    {

        System.IO.StreamWriter file = new System.IO.StreamWriter(errorFolder ....

        ...

    }

    finally {file.Close;}

    It makes sure that the object is disposed of no matter what happens.

    Very usefull for pens, brushes, streams, connections, transactions, and even dialogs.

    see also here: http://msdn.microsoft.com/en-us/library/yh598w02.aspx


    Regards, Nico


    • Edited by Nico Boey Wednesday, February 8, 2012 10:51 AM
    • Proposed as answer by Horizon_Net Wednesday, February 8, 2012 12:12 PM
    • Marked as answer by Pivskid Wednesday, February 8, 2012 1:39 PM
    Wednesday, February 8, 2012 10:44 AM

All replies

  • Hi,

    it's used on objects that implement IDisposable, and it is the equivalent of this:

    try

    {

        System.IO.StreamWriter file = new System.IO.StreamWriter(errorFolder ....

        ...

    }

    finally {file.Close;}

    It makes sure that the object is disposed of no matter what happens.

    Very usefull for pens, brushes, streams, connections, transactions, and even dialogs.

    see also here: http://msdn.microsoft.com/en-us/library/yh598w02.aspx


    Regards, Nico


    • Edited by Nico Boey Wednesday, February 8, 2012 10:51 AM
    • Proposed as answer by Horizon_Net Wednesday, February 8, 2012 12:12 PM
    • Marked as answer by Pivskid Wednesday, February 8, 2012 1:39 PM
    Wednesday, February 8, 2012 10:44 AM
  • Read below page for understanding using blocks,

    http://msdn.microsoft.com/en-us/library/yh598w02.aspx

    http://www.dotnetperls.com/using

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by d347hm4n Wednesday, February 8, 2012 11:42 AM
    Wednesday, February 8, 2012 11:04 AM
  • Shouldn't I expect that the garbage collector takes care of 'disposable's ?

    Wednesday, February 8, 2012 11:42 AM
  • No. You yourself are cleaning up the things by using 'using' blocks. Because, when using block ends, Dispose is automatically called.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, February 8, 2012 11:52 AM
  • The way I will know that the objects implements disposeable resources is to check if a 'close' or a 'dispose'method is present ?

    Wednesday, February 8, 2012 12:12 PM
  • I didn't understand your question correctly. Visit the links I have sent you to know what Dispose does. Coming to your original question, if you don't want to use using block then call Dispose or Close manually.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, February 8, 2012 12:37 PM
  • You can check if class implements IDisposable interface by checking if it can be converted to IDisposable. Method named Close has nothing to do with disposing although some classes that implement IDisposable hide the Dispose method and provide Close method instead or both, but that's not something to rely on. So check that class instance can be converted to IDisposable if you need that check, or check the inheritance chain of the object with some tool like Visual Studio.

    Wednesday, February 8, 2012 12:38 PM