Why we add usage before streaming in C #

why do we put use before streamreader in c#

 using (StreamReader sr = new StreamReader("TestFile.txt")) { string line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } 
+6
source share
8 answers

Using a block in C # is very convenient when working with disposable objects. Disposable objects are those objects that can explicitly release the resources that they use when called for deletion. As you know, NetNet garbage collection is not deterministic, so you cannot predict exactly when an object will be collected by garbage.

Read this post for more details: Understanding block usage in C #

+10
source

So, when you are done using StreamReader , it will be disposed of properly. Also, in the case of an exception, the using statement invokes Dispose before the exception is propagated.

+6
source

It is good practice to use using syntax whenever you work with an object that implements IDisposable (which StreamReader does), as it ensures that the Dispose method is always invoked and the object is correctly deleted.

For example, in this case, various descriptors / locks will be received in the "TestFile.txt" file, which may prevent other users from writing or even reading this file until the stream reader is removed or the process ends. Other objects (for example, database objects) can use database connections or network resources, and therefore you should always dispose of objects as soon as you use them - using the instructions is simple and safe to follow.

Under the covers, what happens is like this ( link ):

 StreamReader sr = new StreamReader("TestFile.txt"); try { string line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } finally { if (sr != null) { ((IDisposable)sr).Dispose(); } } 

However, the using statement is much cleaner (and less error prone) than trying to get rid of IDisposable manually.

+6
source

Without using file will not be closed when it is completed.

+3
source

The code is translated by the compiler into:

 StreamReader sr = new StreamReader("TestFile.txt") try { string line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } finally { sr.Dispose(); } 

This ensures that no matter what happens, sr properly configured (cleared). Whenever an object is created that implements the IDisposable interface, it is best to wrap it in a using construct to make sure it is cleaned up and free up any costly or scarce resources as quickly as possible.

+3
source

Sometimes it is important that we Dispose objects after we are done with them.

Completing the construction of the object in the using block means that the deletion is processed automatically as soon as the code inside the curly braces is completed.

In the case of StreamReader reading a text file, this is important because the file is locked by the system while StreamReader is reading it. Release lock allows other processes to modify or delete the file.

+3
source

using ensures that Dispose() is called and Dispose() calls stream.Close()

+3
source

You do not need to use use, but this is a convenient way to be absolutely sure that the object is properly disposed of. You can do this without using, as long as you are sure that you always delete the object at the end ( using a, finally, the normal path). If there are special reasons to continue searching for an object, you can do this.

However, if you are trying to do this, there is something with your code structure. The designation for using StreamReader (and other iDisposible objects) in the using statement helps structure your code well.

+2
source

Source: https://habr.com/ru/post/892565/


All Articles