Which code segment should you insert at line 07?

You are developing an application by using C#. The application includes the following code segment. (Line numbers are included for reference only.)

The DoWork() method must throw an InvalidCastException exception if the obj object is not of type IDataContainer when accessing the Data property.
You need to meet the requirements. Which code segment should you insert at line 07?
A. var dataContainer = (IDataContainer) obj;
B. var dataContainer = obj as IDataContainer;
C. var dataContainer = obj is IDataContainer;
D. dynamic dataContainer = obj;

microsoft-exams

2 thoughts on “Which code segment should you insert at line 07?

  1. Correct is D. See req throw ex “when accessing the Data property” – its line 10! In case A exeption will in line 07.

    1. >> its line 10! In case A exeption will in line 07.
      In this case the RunTimeBinderException will be thrown, not InvalidCastException. Just checked in code.

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.