TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# using Statement: Dispose and IDisposable

Understand the using statement. Handle IDisposable types like StreamReader with using.
Using. The using block helps manage resources. It protects the whole system's resources by specifying the scope of the usage of the resource.
The using statement is combined with a type that implements IDisposable. Things like StreamReader or StreamWriter implement IDisposable.StreamReaderStreamWriterInterface
To begin, this program defines a class called SystemResource. The class implements the IDisposable interface and the required Dispose method.

Note: In the Main method, we wrap the SystemResource instance inside a using statement.

On execution: First the SystemResource instance is allocated upon the managed heap. Next the "1" is written.

And: The "0" is written because the Dispose method is invoked. And finally, the "2" is printed.

Info: As demonstrated, the Dispose method is called immediately when control flow exits the using block.

C# program that uses using statement using System; using System.Text; class Program { static void Main() { // Use using statement with class that implements Dispose. using (SystemResource resource = new SystemResource()) { Console.WriteLine(1); } Console.WriteLine(2); } } class SystemResource : IDisposable { public void Dispose() { // The implementation of this method not described here. // ... For now, just report the call. Console.WriteLine(0); } } Output 1 0 2
Example 2. It is possible to nest multiple using statements one after another. You do not need to use any curly brackets in this case.

Tip: In the second or further using statements, you can use the variables declared in previous using statements as well.

C# program that nests using blocks using System; class Program { static void Main() { using (SystemResource resource1 = new SystemResource()) using (SystemResource resource2 = new SystemResource()) { Console.WriteLine(1); } } } class SystemResource : IDisposable { public void Dispose() { Console.WriteLine(0); } }
Notes, performance. If we omit "using" or Dispose calls, we may improve performance—until the code stops working. So it is best to add "using" when it is supported.
Discussion. It is most common to use "using" with types already defined in the .NET Framework. Some of these types include BinaryReader, BinaryWriter, DataTable.BinaryReaderBinaryWriterDataTable

Info: Use a resource acquisition expression inside the using statement. You do not need to implement any interfaces.

Summary. The using statement invokes the Dispose method found in the IDisposable interface implementation. This article does not describe a useful implementation of Dispose.
Review, Dispose. The using statement interacts with Dispose. It affects the path of the control flow in the virtual execution engine.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf