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?

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.
dataContainer = obj as IDataContamer;

C.
var dataContainer = obj is IDataContainer;

D.
dynamic dataContainer = obj;

Explanation:
another version of one of the question. this is diffrent beacause you need to throw InvalidCastException



Leave a Reply 3

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


MarcoJacob

MarcoJacob

Oh sry its A cause a requirement is, that the exception is thrown if type is wrong, so “var dataContainer = (IDataContainer) obj” will do this if the type differs from IDataContainer

Artem

Artem

Only A can throw InvalidCastException, but what about “when accessing the Data property”…
D can throw exception “when accessing the Data property”, but exception type is RuntimeBinderException…