What should you do?

You work as an application developer at Domain.com. Domain.com wants you to develop an application
that stores and retrieves staff information by means of a unique staff number. You have already written the following code for the purpose of storing Employee objects.
Employee e1 = new Employee (1001, “Andy Reid”, “Manager”);
Employee e2 = new Employee (1002, “Kara Lang”, “Sales Engineer”);
Dictionary eData = new Dictionary ();
eData.Add (e1.ID, e1);
eData.Add (e2.ID, e2);
All other Employee objects have been added in the same way. You are required to display all key/value pairs within the Dictionary collection.
What should you do?

You work as an application developer at Domain.com. Domain.com wants you to develop an application
that stores and retrieves staff information by means of a unique staff number. You have already written the following code for the purpose of storing Employee objects.
Employee e1 = new Employee (1001, “Andy Reid”, “Manager”);
Employee e2 = new Employee (1002, “Kara Lang”, “Sales Engineer”);
Dictionary eData = new Dictionary ();
eData.Add (e1.ID, e1);
eData.Add (e2.ID, e2);
All other Employee objects have been added in the same way. You are required to display all key/value pairs within the Dictionary collection.
What should you do?

A.
Use the following code:
foreach (KeyValuePair keyPair in eData)
Console.WriteLine (“{0} key : {1} value”, keyPair.Key, keyPair.Value);

B.
Use the following code:
foreach (string key in eData.Keys)
Console.WriteLine (“{0} key : {1} value”, Key, (Employee) eData [key]);

C.
Use the following code:
foreach (KeyValuePair keyPair in eData)
Console.WriteLine (“{0} key : {1} value”, keyPair.Key, keyPair.Value);

D.
Use the following code:
foreach (object value in eData.Values)
Console.WriteLine (“{0} key : {1} value”, eData [value], value);

Explanation:
This code iterates through each KeyValuePair structure in the generic DictionaryData, and it displays the Key and Value properties. Like the non-generic IDictionary interface, the key is used to retrieve the value. Unlike the non-generic IDictionary interface, the key does not have to be a string and the value does not have to be a generic object. You must specify the CKey and TValue placeholders when specifying a KeyValuePair structure. Because the eDataDictionary collection is instantiated with the integer and Employee data types for the CKey and TValue placeholders, respectively, the KeyValuePair structure should also use these data types. During each iteration, the KeyValuePair object is assigned to the keyPair variable, and the Console.WriteLine method is used to display the Keyand Value properties to the console.
Incorrect Answers:
B: Like the non-generic IDictionary interface, the key is used to retrieve the value. Unlike the non-generic IDictionary interface, the key does not have to be a string and the value does not have to be a generic object. You must specify the CKey and TValue placeholders when specifying a KeyValuePair structure.
C: You should not use the code that does not specify the CKey and TValue placeholders when using the KeyValuePair structure because the data types must be declared explicitly.
D: You should not use the code that specifies a value when accessing items in the Dictionary collection because you should use a key to access a value and you cannot guarantee that only one key exists for a value, as there might be duplicate values in a Dictionary collection



Leave a Reply 1

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