nerdexam
Microsoft

98-372 · Question #113

You are creating an application using .NET Framework 4.0. You write the following code segment in the application. class CompGenerate <T> where T : IComparable { public T t1; public T t2; public…

The correct answer is C. CompGenerate<double> gen = new CompGenerate<double>(10.125, 2005). The following constructor is correct: CompGenerate<double> gen = new CompGenerate<double>(10.125, 2005); The given class declaration is a generic. Therefore, in order to create a generic, you need to provide a type for each generic type, and that type must meet all constraints…

Understanding Data Types and Collections

Question

You are creating an application using .NET Framework 4.0. You write the following code segment in the application. class CompGenerate <T> where T : IComparable { public T t1; public T t2; public CompGenerate(T _t1, T _t2) { t1 = _t1; t2 = _t2; } } Given the above class declaration, which of the following constructors is correct?

Options

  • ACompGenerate<float> gen = new CompGenerate<float>();
  • BCompGenerate gen = new CompGenerate(10.125, 2005);
  • CCompGenerate<double> gen = new CompGenerate<double>(10.125, 2005);
  • DCompGenerate<double, float> gen = new CompGenerate<double, float>(20.125, 10.525);

How the community answered

(60 responses)
  • A
    10% (6)
  • B
    2% (1)
  • C
    83% (50)
  • D
    5% (3)

Explanation

The following constructor is correct: CompGenerate<double> gen = new CompGenerate<double>(10.125, 2005); The given class declaration is a generic. Therefore, in order to create a generic, you need to provide a type for each generic type, and that type must meet all constraints. The .NET Framework version has provided a new feature of generics that introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that postpone the specification of one or more types until the class or method is declared and instantiated by client code. For instance, by using a generic type parameter T, a single class can be written that other client code can exploit without incurring the cost or risk of runtime casts or boxing operations. The following are the benefits of using generics: 1.Generic types are used to maximize the reusability of code, type safety, and performance of .NET applications. 2.Generics are used to create new generic collection classes contained in the System.Collections.Generic namespace. 3.Custom generics can be created that include generic interfaces, classes, methods, events and 4.The type information used in a generic data type can be obtained by reflection during runtime. 5.Generics enable the compiler to catch type-casting errors during compilation. 6.Generics do not require casting or boxing, and therefore, it improves runtime performance. Answer: A is incorrect. In order to create an instance of a generic class, you need to provide parameter values. This constructor does not provide parameter values. Answer: D is incorrect. The given class declaration only specifies a single generic type. This constructor provides two generic types. Answer: D is incorrect. The given class declaration only specifies a single generic type. This constructor provides two generic types. Answer: B is incorrect. In order to create an instance of a generic class, you need to provide a type. This constructor does not provide a type.

Topics

#generics#type constraints#IComparable#constructor syntax

Community Discussion

No community discussion yet for this question.

Full 98-372 Practice