C# Programming - Generics - Discussion

Discussion :: Generics - General Questions (Q.No.2)

2. 

For the code snippet shown below, which of the following statements are valid?

public class Generic<T>
{
    public T Field; 
    public void TestSub()
    {
        T i = Field + 1;
    }
}
class MyProgram
{
    static void Main(string[] args)
    {
        Generic<int> gen = new Generic<int>();
        gen.TestSub();
    }
}

[A]. Addition will produce result 1.
[B]. Result of addition is system-dependent.
[C]. Program will generate run-time exception.
[D]. Compiler will report an error: Operator '+' is not defined for types T and int.
[E]. None of the above.

Answer: Option D

Explanation:

No answer description available for this question.

Isha Lal said: (Sep 29, 2011)  
It will show a compiler error because the statement
"Field + 1" is incorrect.
Operand + is applied to 2 different datatypes, int and T.
Operand + is defined only for same datatypes.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.