C# Online Compiler
Example: Addition of Two Numbers in C# using Generics
C
C++
C#
Java
Python
PHP
main.cs
STDIN
Run
// Addition of Two Numbers in C# using Generics using System; public class W3CW { // To make the sum of two numbers <Generic method> public int Sum<T>(T x, T y) { dynamic a=x; dynamic b=y; return a+b; } // @It's the driver function static public void Main (string[] args) { Console.WriteLine("Enter two integer values::"); int p = Convert.ToInt32(Console.ReadLine()); int q = Convert.ToInt32(Console.ReadLine()); W3CW Obj = new W3CW(); int sum = Obj.Sum<int>(p, q); // It will print the final output Console.Write("\nResult:: " + p + " + " + q + " = " + sum); } }
10 20
Output
Clear
ADVERTISEMENTS