For the example on the code below, which of the following statements are true?
public class MyContainer
{
//Insert code here
}
I. MyContainer class demands that the generic parameter implements IComparable interface.
II. The compiler will report an error for that code block.-False
III. There are multiple constraints on MyContainer generic parameter definition. -True
IV. MyContainer class demands that the generic parameter is of reference type and also implements the IComparable interface. - True
a) I and II only
b) I, III and IV only
c) II and III only
d) All above
e) None of the above
Dhirendra MisraPosted Dec 30, 2013, 12:14 AM
The statements III and IV are true. The statement 1 is not true as it explains incomplete behaviour.
You can try using following examples:
//Calling from Main without implementing IComparable
genericClass
Compilation error : 'Corner.EmployeeWithoutIComparable' cannot be used as type parameter 'T' in the generic type or method 'Corner.genericClass
//Calling from Main with struct and implementing IComparable
genericClass
Compilation error : The type 'Corner.EmployeeStruct' must be a reference type in order to use it as parameter 'T' in the generic type or method 'Corner.genericClass
//Calling from Main with class and implementing IComparable
Hope it makes clear to you.
Thanks