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

C.
var dataContainer = obj is IDataContainer;

D.
dynamic dataContainer = obj;

Explanation:
http://msdn.microsoft.com/en-us/library/ms173105.aspx



Leave a Reply 9

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


Marco

Marco

It is B. Classic cast throws exception if cast failts, “as” returns null if cast fails.

Pete

Pete

Answer A is correct. The questions states that an InvalidCastException must be thrown if the object is not of type IDataContainer.

Zach

Zach

Yup definitely B.

Najlepszy Programista Swiata DAGO

Najlepszy Programista Swiata DAGO

A!!!!

Twórca C#

Twórca C#

I’m C# creator and the correct answer is A barany!!!!

Lord Vader

Lord Vader

yes definitely A. Marco hasnt read it properly.