473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Why operation not allowed with generic data types

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 T Add<T>(T a,T b)
{
return (a + b);
}

}
}

It gives the 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!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
4 1329
mdb
Raju Shrestha <ra**********@yahoo.com> wrote in news:#tiytV7lEHA.3612
@TK2MSFTNGP12.phx.gbl:
It gives the 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!


The whole point is that the compiler doesn't know what a 'T' is. Just
because you happened to use an 'int' and a 'float' which do support the +
operator, there's no way it can tell that you won't try to do Math.Add
<boolean>, which doesn't support the + operator.

On a side note, you probably shouldn't use the name 'Math' for the class
since that's already used by the .NET framework. Unless of course, you're
just trying to make things simple for us.... :)

-mdb
Nov 16 '05 #2
It looks like you need to overload the + operator in order to provide the
necessary details to the runtime to be able to add the two types together.

HTH,

Greg

"Raju Shrestha" wrote:
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 T Add<T>(T a,T b)
{
return (a + b);
}

}
}

It gives the 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!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Raju Shrestha wrote:

You can't just overload the '+' operator as someone suggested, because
the compiler has no idea what type T will be until runtime.
Nov 16 '05 #4


"Jeremy Gailor" wrote:
Raju Shrestha wrote:

You can't just overload the '+' operator as someone suggested, because
the compiler has no idea what type T will be until runtime.


Good point. I really have to get into Generics more.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
6
by: LordHog | last post by:
Hello all, My lead wants to implement a data range monitor for a project that we are coding. Basically it performs a boundry checking that will take three parameters. I am/was trying to...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
11
by: HairlipDog58 | last post by:
Hello, There are several 'cross-thread operation not valid' exception postings in the MSDN discussion groups, but none address my exact situation. I have authored a .NET component in Visual C#...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
0
by: Default User | last post by:
I work on creating test cases for a SOAP-based set of servers, using soapUI. I received and updated set of WSDL and schema files, and when I made new tests and mock server operations, all of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.