1

I'm doing a tutorial on how to save a text file with StreamWriter. I understand how to do it but I started reading a tutorial on the StreamWriter Class to get a better understanding of it. In an example, it places the word using StreamWriter but in the other tutorial, it does not. Why is this? Do I need to use the using?

how to save to text tutorials way

private void button3_Click(object sender, EventArgs e)
        {
            StreamWriter File = new StreamWriter("data.txt", true);

            File.Write("");
            File.Close();
        }

== StreamWriter Class In C# With Programming Example ==

namespace StreamWriter_Class
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = @"D:\csharpfile.txt";
        --->    using (StreamWriter writer = new StreamWriter(file))
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    Using `Using` will allow for automatic garbage collection. See [this answer](https://stackoverflow.com/a/10057366/1350913) – IronAces Dec 04 '18 at 16:54
  • 1
    Using is used for classes that implement IDisposable. It is the same as wrapping the block of code in a try/catch/finallyblock and running the .Dispose method in the 'finally' section. If you don't do this, it can cause memory leaks. Using is the preferred way of doing this. – Jon Vote Dec 04 '18 at 16:54
  • Thank you for the quick responses. There are a lot of good tutorials out there, but they don't explain the little things like that too well or at all. –  Dec 04 '18 at 17:04
  • @DanielShillcock that is a mis-statement; `using` doesn't impact the garbage collection **at all**. Disposal is different to collection. – Marc Gravell Dec 06 '18 at 09:06

3 Answers3

2

using is a syntactic sugar that makes sure that the resource you are using (no pun intended!) will be Dispose()d , no matter if the actions that were performed on it threw any exception (which is not uncommon when handling external resources).

WHile it's not strictly "wrong" not to use it, it's a very good practice, and you should probably adopt it wherever possible.

See the official documentation for additional details.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Every using will become something like that:

StreamWriter writer = new StreamWriter(file);
try
{
    // do something with writer
}
finally
{
    if(writer != null)
        writer.Dispose(); // close handle and free resorces
}

Always your writer handle and resources will be closed and free and GC don't need to check that.

You can use using in yours own classes implementing IDisposable interface.

See:

https://www.dotnetperls.com/using

When should I use "using" blocks in C#?

Implementing IDisposable correctly

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch-finally

Community
  • 1
  • 1
Kevin Kouketsu
  • 786
  • 6
  • 20
1

using will make sure that the object (in this case StreamWriter) will be disposed, when it is out-of-scope.

There are posted plenty of examples with explanations in this question: What are the uses of "using" in C#?

And here is a really good explanation from DotNetPerls: DotNetPerls - Using

TheSteelDuck
  • 165
  • 1
  • 9