none
Using and Dispose() - I can't apply them.

    Question

  • The next codes get errors:

    1.

        class Program
        {
            static void Main(string[] args)
            {
                using (String str = String.Empty)
                {
    
                }
            }
        }

    2.

        class Program
        {
            static void Main(string[] args)
            {
                String str = String.Empty;
                try
                {
    
                }
                finally
                {
                    ((IDisposable)str).Dispose();
                    str.Dispose();
                }
            }
        }

    Saturday, August 09, 2014 11:08 AM

Answers

  • You never explictly dispose a String object in C# as it doesn't implements the IDisposable interface. You can only use a using statement for types that implements the IDisposable interface and the String class does not. Therefore you cannot declare and instantiate a String object in a using statement.

    This is clearly stated in the documentation of the using statement the following MSDN link.

    using Statement (C# Reference): http://msdn.microsoft.com/en-us//library/yh598w02.aspx

    Saturday, August 09, 2014 1:34 PM
  • If you want to use using and Dispose() only, you can write as follows:

    1.

     class Program
       
    {
           
    static void Main(string[] args)
           
    {
               
    using ( Component com = new Component())
               
    {

                }
           
    }
       
    }

    2.

    class Program
       
    {
           
    static void Main(string[] args)
           
    {
               
    Component com = new Component();
               
    try
               
    {

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

    • Marked as answer by Ziya Ceferov Tuesday, August 12, 2014 7:23 AM
    Saturday, August 09, 2014 2:58 PM

All replies

  • Hello,

    You use a string then when it's out of scope it will be marked to be taken care of by the garbage collector. See the following post.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Saturday, August 09, 2014 11:34 AM
    Moderator
  • I wanna use Using and Dispose(), can you write a code of it?
    Saturday, August 09, 2014 12:20 PM
  • Hello, You don't need and there is no implémentation for idisposable method in string. Garbage collector will detect if it stills a reference on the string and if it's not the case, reference is removed. Disposable interface is used for unmanaged resources. Regards Cedric
    Saturday, August 09, 2014 1:09 PM
  • I wanna use Using and Dispose(), can you write a code of it?
    If you are still asking this question then please go back to my first reply and read the information at the link I provided.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Saturday, August 09, 2014 1:27 PM
    Moderator
  • You never explictly dispose a String object in C# as it doesn't implements the IDisposable interface. You can only use a using statement for types that implements the IDisposable interface and the String class does not. Therefore you cannot declare and instantiate a String object in a using statement.

    This is clearly stated in the documentation of the using statement the following MSDN link.

    using Statement (C# Reference): http://msdn.microsoft.com/en-us//library/yh598w02.aspx

    Saturday, August 09, 2014 1:34 PM
  • If you want to use using and Dispose() only, you can write as follows:

    1.

     class Program
       
    {
           
    static void Main(string[] args)
           
    {
               
    using ( Component com = new Component())
               
    {

                }
           
    }
       
    }

    2.

    class Program
       
    {
           
    static void Main(string[] args)
           
    {
               
    Component com = new Component();
               
    try
               
    {

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

    • Marked as answer by Ziya Ceferov Tuesday, August 12, 2014 7:23 AM
    Saturday, August 09, 2014 2:58 PM