From the course: C# Best Practices for Developers

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Using statement for efficiency

Using statement for efficiency - C# Tutorial

From the course: C# Best Practices for Developers

Start my 1-month free trial

Using statement for efficiency

- [Instructor] So, I'd like to talk about one more feature that you should keep in mind when you are incorporating best practices. That is implementing the using statement. And I'm not referring to the keyword using at the first line here where it's used to import namespaces. I'm referring to the keyword using that handles and cleans up resources. It's used to declare a scope to use our resources, and then it disposes of them. It's generally good for stream and database connections. And it essentially can take the place of work that you would put into a finally block, which is used to clean house. I'm gonna go ahead and demonstrate what I'm referring to. Let's say that I wanted to use filestream to open up a file. Now, I have a file in my temp directory called theAgency and it just has some repeated content in it. And we're gonna go ahead and use filestream to open it. I'll start off by commenting out our previous code and typing filestream. I'll hold control and period to import the…

Contents