PrepAway - Latest Free Exam Questions & Answers

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.)
01 public interface IDataContainer
02 {
03 string Data { get; set; }
04 }
05 void DoWork(object obj)
06 {
07
08 if (dataContainer != null)
09 {
10 Console.WriteLine(dataContainer.Data);
11 }
12 }
The DoWork() method must not throw any exceptions when converting the obj object to the
IDataContainer interface or when accessing the Data property. You need to meet the requirements. Which
code segment should you insert at line 07?

PrepAway - Latest Free Exam Questions & Answers

A.
var dataContainer = (IDataContainer)obj;

B.
dynamic dataContainer = obj;

C.
var dataContainer = obj is IDataContainer;

D.
var dataContainer = obj as IDataContainer;

Explanation:
as – The as operator is like a cast operation. However, if the conversion isn’t possible, as returns null instead of
raising an exception. http://msdn.microsoft.com/en-us/library/cscsdfbt(v=vs.110).aspx


Leave a Reply