From the course: Transition from C# to Python

Unlock the full course today

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

Scope blocks

Scope blocks

From the course: Transition from C# to Python

Start my 1-month free trial

Scope blocks

- [Instructor] Both Python and C-Sharp supports something called scope blocks. Which can be used to automatically release resources when they're no longer needed for an operation. An example of this in C-Sharp is to close a file stream when information is finished being read or written. So let's open up the C-Sharp example first to give you a sense of this. So, C-Sharp has the using statement to implement this. So here in this C-Sharp example, we can see that the using statement opens a stream reader to read the contents of a text file and then writes out the number of lines in the text file. And when the closing brace of the using block is reached, the resources associated with the stream reader are then automatically released. And you can do this with any object that implements the IDisposable interface. So let's go ahead and run this. So here in the terminal, I'm in chapter five, and I'm going to go into scopes, and…

Contents