Releasing Resource & The Syntax Candy

In C# practice, we may need to read or retrieve information from a text file; such as .text, .pdf and etc.

It is a good practice of critical thinking in this scenario, what if other objects or classes need to read this file?
The solution can be simple, and we can close the file or source after we use it, and other objects or class can use it after us. It is a common pattern in C# language, and I also believe this will benefit us when we work on a project with other programmers.

Case Study

We are writing a code snippet which will read a text file from our drive and other objects need to read it after us. So, we need to close it after we read it.

Talk Is Cheap, Show Me The Code

Another Approach

The code above does the work very well; however, it is a bit lengthy. Thanks for C#’s Syntax Candy. Syntax Candy does work for us behind the scene in C#, which saves us a lot of effort when we need to write a relatively large project.

In the above example, we use the keyword using as a syntax candy to rewrite the first code snippet. In the end, the file will be closed automatically.

3 Replies to “Releasing Resource & The Syntax Candy”

  1. I’m also commenting to let you be aware of of the excellent discovery my child encountered checking your blog. She came to understand a wide variety of details, most notably how it is like to have a marvelous coaching heart to get folks easily fully grasp a variety of complicated matters. You actually did more than my desires. I appreciate you for delivering such great, dependable, edifying and also cool guidance on that topic to Evelyn.

  2. Thank you for your entire labor on this web site. My mom take interest in participating in investigations and it is obvious why. My partner and i learn all regarding the lively tactic you produce simple thoughts through your web site and in addition invigorate response from the others on the subject then our favorite princess is truly studying a whole lot. Take advantage of the rest of the year. You have been doing a superb job.

  3. I am commenting to let you know of the fantastic encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people quite simply grasp chosen grueling subject areas. You really exceeded our expectations. Many thanks for giving the informative, healthy, educational and as well as easy thoughts on the topic to Jane.

Leave a Reply

Your email address will not be published. Required fields are marked *