2
Answers

Problem of Adding two Numbers Using Generics

Please Help me to solve this problem

The below code is not working. An error occurred during compilation. The error message as follows.

 Operator '+' cannot be applied to operands of type 'T' and 'T'

//My Code Here....

  namespace GenericClass{

    class Program {

        static void Main(string[] args) {

            Gen<int> genint = new Gen<int>();

            genint.Add(5, 6);

            Gen<float> genfloat = new Gen<float>();

            genfloat.Add(3.4F, 3.5F); 

        }

 

   class Gen<T>

        {

            public void Add(T a1, T a2)

            {

                T c1=a1+a2;

                Console.WriteLine(c1.ToString());

           }

         }

    }

}

Answers (2)