C# Programming - Generics - Discussion

Discussion Forum : 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();
    }
}
Addition will produce result 1.
Result of addition is system-dependent.
Program will generate run-time exception.
Compiler will report an error: Operator '+' is not defined for types T and int.
None of the above.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
1 comments Page 1 of 1.

Isha Lal said:   1 decade ago
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.
(1)

Post your comments here:

Your comments will be displayed after verification.