Which code segment should you use?

You are developing an application that will read data from a text file and display the file contents.
You need to read data from the file, display it, and correctly release the file resources.
Which code segment should you use?

You are developing an application that will read data from a text file and display the file contents.
You need to read data from the file, display it, and correctly release the file resources.
Which code segment should you use?

A.
Option A

B.
Option B

C.
Option C

D.
Option D



Leave a Reply 6

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


Lord Vader

Lord Vader

it cant be A.

Lord Vader

Lord Vader

has to be D. the dispose main purpose is to clean up unmanaged resources (resources outside the control of the CLR)

abc

abc

True, but the dispose should have been called in the finally block not at the end of the try block since we might not reach it. Aka it’s the main reason the reader has been declared outside of the try block so it could be accessed in the finally.

It’s A
a using statement can only be used when the class implements IDisposable interface, the benefit is once we go outside of the using the object is automatically disposed.

Pavel

Pavel

A is correct because it use using statement for a StreamReader.