98-372 · Question #78
You are creating an application using .NET Framework 4.0. You write the following code segment in the application: NewClass x; bool y; x = 42; // Convert using ToBoolean. y = Convert.ToBoolean(x)…
The correct answer is B. IConvertible. The custom class NewClass should implement the IConvertible interface. Use the IConvertible interface to enable a custom class to be converted to other types. The IConvertible interface of the System namespace defines methods that convert the value of an object of an…
Question
You are creating an application using .NET Framework 4.0. You write the following code segment in the application:
NewClass x; bool y; x = 42; // Convert using ToBoolean. y = Convert.ToBoolean(x); Console.WriteLine("x = {0}, y = {1}", x.ToString(), y.ToString()); Which interface should the custom class NewClass implement?
Options
- AIComparable<T>
- BIConvertible
- CFormatterConverter
- DIComparable
How the community answered
(44 responses)- A5% (2)
- B84% (37)
- C9% (4)
- D2% (1)
Explanation
The custom class NewClass should implement the IConvertible interface. Use the IConvertible interface to enable a custom class to be converted to other types. The IConvertible interface of the System namespace defines methods that convert the value of an object of an implementing type into a common language runtime (CLR) type that has an equivalent value. The CLR uses the Convert class for exposing the IConvertible interface. It uses types that include Boolean, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime, Char, and String. Some of the methods defined by this interface are ToBoolean, ToDateTime, ToSingle, ToString, etc. The ToBoolean method converts the value of an object that is equivalent to the CLR Boolean value by using the specified culture-specific formatting information. The ToDateTime method converts the value of an object that is equivalent to the CLR DateTime value by using the specified culture-specific formatting information. Similarly, the ToSingle method converts the value of an object that is equivalent single-precision floating-point number by using the specified culture-specific formatting information. The ToString method converts the value of an object that is equivalent to the CLR String value by using the specified culture-specific formatting information. Answer: D is incorrect. Implementing the IComparable interface does not allow a class to be converted. The IComparable interface of the System namespace is used to define a generalized comparison method. When a value type or class implements this interface, it creates a type- specific comparison method that is the CompareTo method. The CompareTo method compares the current object of a class with another object of the same type. The IComparable interface is also suitable for the sorting mechanism. Answer: A is incorrect. Implementing the IComparable<T> interface does not allow a class to be Answer: C is incorrect. The FormatterConverter class is used to represent a base implementation of the IFormatterConverter interface. The IFormatterConverter interface uses the Convert class and the IConvertible interface. Any public static members of this type are thread safe. However, any instance members are not guaranteed to be thread safe.
Topics
Community Discussion
No community discussion yet for this question.