raju_shrestha
Member
- Joined
- Nov 23, 2003
- Messages
- 14
I am trying to learn Generics with C# and tried following code:
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace Generics
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("5+2 = " + Math.Add<int>(5,6));
Console.WriteLine("1.2 + 3.3 = " + Math.Add<float>(1.2F, 3.3F));
Console.ReadLine();
}
}
class Math
{
public static T Add<T>(T a,T b)
{
return (a + b);
}
}
}
It gives the compile time error message "Operator + cannot be applied to operands of type T and T". Is this C# bug or something else. Appreciate for experts help.
thanks!
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace Generics
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("5+2 = " + Math.Add<int>(5,6));
Console.WriteLine("1.2 + 3.3 = " + Math.Add<float>(1.2F, 3.3F));
Console.ReadLine();
}
}
class Math
{
public static T Add<T>(T a,T b)
{
return (a + b);
}
}
}
It gives the compile time error message "Operator + cannot be applied to operands of type T and T". Is this C# bug or something else. Appreciate for experts help.
thanks!
Last edited by a moderator: