473,396 Members | 1,872 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,396 software developers and data experts.

Generic methods..

Ram
Hi All,

I am new to this Generics. I started with a Generic method which
doesn't return any value. It worked well.

Then I tried to write a generic method which will sum up the given
values and return the result. This is the piece of code.

private T Add<T>(T a, T b)
{

T c = a + b;
return c;
}

While compilation, I am getting the following error.

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

What is the problem?. How to perform operations on the Generic
variables?

How to write generic methods that will return a value?

Please throw some light on this.

Thanks in advance.
Regards,
Ram

Mar 15 '07 #1
4 2113
A common question. Generics are not like C++ templates, and the short
answer is that you can't use operator overloads (such as +, -, etc)
with gnerics. You can use other constraints, however, such as
"implements <xinterface", "is reference-type", "has a default
colnstructor". They are very useful, but not directly in your scenario
I'm afraid.

Marc

Mar 15 '07 #2
True. There are some things that can be done but not the C++ way at all.

For example your Add *could* check common types and behave as the best.
The only thing is that if someone calls the function with a type you have
not implemented (say Add<Form>), it will not work.

public class Template
{
public T Add<T>(T a1, T a2) where T:class
{
if (a1 is string && a2 is string)
{
return string.Concat(a1, a2) as T;
}
else if (a1 is int && a2 is int)
{
return (Convert.ToInt32(a1) + Convert.ToInt32(a1)) as T;
}

return default(T);
}
}

static void Main(string[] args)
{
Template x = new Template();
int v = (int)x.Add<object>(2, 3);
}
"Marc Gravell" <ma**********@gmail.comha scritto nel messaggio
news:11*********************@y66g2000hsf.googlegro ups.com...
>A common question. Generics are not like C++ templates, and the short
answer is that you can't use operator overloads (such as +, -, etc)
with gnerics. You can use other constraints, however, such as
"implements <xinterface", "is reference-type", "has a default
colnstructor". They are very useful, but not directly in your scenario
I'm afraid.

Marc

Mar 15 '07 #3
This example would be better handled with overloading. The whole point
of generics is that you *expect* it to be called for typed you don't
know about; also - unless I am very mistaken it can't work with int
because of the T : class

public int Add(int a1, int a2) {return a1 + a2;}
public string Add(string a1, string a2) {return a1 + a2;} // Concat by
the compiler
etc

Marc

Mar 16 '07 #4
A true generic implementation must support types that don't exist in
the (base) library wrapping the code. Maybe I am overcomplicating
things, but you could do this via interfaces etc as follows. You could
add a few more "standard" implementations, but more importantly the
end-developer can hook their own definitions for complex, quoternian,
SomeClass, SomeStruct, etc...

Anyways, here it is: (this is just a demo... not production code)

using System;
using System.Collections.Generic;
using System.Text;
static class Program {
static void Main() {
Console.WriteLine(Foo(1, 4, 6));
Console.WriteLine(Foo(12.0F, 1.2F, 6F));
Console.WriteLine(Math.Add("abc","def", "ghi"));
try {
Console.WriteLine(Foo("abc", "def", "ghi"));
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
static T Foo<T>(T arg1, T arg2, T arg3) {
return (Math.Product(Math.Add(arg1, arg2), arg3));
}

}
public interface IMath<T{
T Add(T lhs, T rhs);
T Add(params T[] args);
T Product(T lhs, T rhs);
T Product(params T[] args);
T Negate(T arg);
T Invert(T arg);
T Unit { get;}
T Zero { get;}
// etc
}
public abstract class MathBase<T: IMath<T{
protected T ThrowNotSupported(string name) {
throw new NotSupportedException(name + " is not supported for
" + typeof(T).FullName);
}
virtual public T Add(params T[] args) {
if (args == null) throw new ArgumentNullException("args");
IMath<Tprovider = Math<T>.Default;
switch(args.Length) {
case 0: return provider.Zero;
case 1: return args[0];
case 2: return provider.Add(args[0], args[1]);
}

T value = provider.Add(args[0], args[1]);
for (int i = 2; i < args.Length; i++) {
value = provider.Add(value, args[i]);
}
return value;
}
virtual public T Product(params T[] args) {
if (args == null) throw new ArgumentNullException("args");
IMath<Tprovider = Math<T>.Default;
switch(args.Length) {
case 0: return provider.Unit;
case 1: return args[0];
case 2: return provider.Product(args[0], args[1]);
}

T value = provider.Product(args[0], args[1]);
for(int i = 2; i < args.Length ; i++) {
value = provider.Product(value, args[i]);
}
return value;
}
virtual public T Add(T lhs, T rhs) {
return ThrowNotSupported("Add");
}
virtual public T Product(T lhs, T rhs) {
return ThrowNotSupported("Product");
}
virtual public T Negate(T arg) {
return ThrowNotSupported("Negate");
}
virtual public T Invert(T arg) {
return ThrowNotSupported("Invert");
}
virtual public T Unit {
get { return ThrowNotSupported("Unit");}
}
virtual public T Zero { // this is actually a reasonable default
implementation
get { return default(T); }
}
}
internal static class Math<T{ // avoid using the dictionary on each
access
private static readonly IMath<Tprovider;
public static IMath<TDefault { get { return provider; } }
static Math() {
provider = Math.GetHandler<T>();
}

}
public static class Math {
public static T Add<T>(T lhs, T rhs) {
return Math<T>.Default.Add(lhs, rhs);
}
public static T Add<T>(params T[] args) {
return Math<T>.Default.Add(args);
}
public static T Product<T>(T lhs, T rhs) {
return Math<T>.Default.Product(lhs, rhs);
}
public static T Product<T>(params T[] args) {
return Math<T>.Default.Product(args);
}
public static T Negate<T>(T arg) {
return Math<T>.Default.Negate(arg);
}
public static T Invert<T>(T arg) {
return Math<T>.Default.Invert(arg);
}
public static T Unit<T>() {
return Math<T>.Default.Unit;
}
public static T Zero<T>() {
return Math<T>.Default.Zero;
}

static Math() {
providers = new Dictionary<Type, object>();
AddProvider<int>(new MathInt32Provider());
AddProvider<float>(new MathSingleProvider());
AddProvider<string>(new MathStringProvider());
}
internal sealed class MathInt32Provider : MathBase<int{
override public int Add(int lhs, int rhs) {return lhs + rhs;}
override public int Product(int lhs, int rhs) { return lhs *
rhs; }
override public int Negate(int arg) { return -arg; }
override public int Invert(int arg) { throw new
NotSupportedException(); }
override public int Unit { get { return 1; } }
}
internal sealed class MathSingleProvider : MathBase<float{
override public float Add(float lhs, float rhs) { return lhs +
rhs; }
override public float Product(float lhs, float rhs) { return
lhs * rhs; }
override public float Negate(float arg) { return -arg; }
override public float Invert(float arg) { return 1.0F / arg; }
override public float Unit { get { return 1.0F; } }
}
internal sealed class MathStringProvider : MathBase<string{
override public string Add(string lhs, string rhs) { return
lhs + rhs; }
override public string Zero { get { return ""; } }
public override string Add(params string[] args) {
if (args == null) throw new ArgumentNullException("args");
switch (args.Length) {
case 0: return "";
case 1: return args[0];
case 2: return string.Concat(args[0], args[1]);
case 3: return string.Concat(args[0], args[1],
args[2]);
case 4: return string.Concat(args[0], args[1],
args[2], args[3]);
}
StringBuilder sb = new StringBuilder();
foreach (string arg in args) {
sb.Append(arg);
}
return sb.ToString();
}
}
static readonly Dictionary<Type, objectproviders;
public static void AddProvider<T>(IMath<Tprovider) {
if(provider==null) throw new
ArgumentNullException("provider");
lock (provider) {
providers.Add(typeof(T), provider);
}
}
internal static IMath<TGetHandler<T>() {
lock (providers) {
object provider;
if (!providers.TryGetValue(typeof(T), out provider)) {
throw new NotSupportedException("No IMath<Tprovider
exists for " + typeof(T).FullName);
}
return (IMath<T>)provider; // cast is OK since it made it
in
}
}
}
Mar 16 '07 #5

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

Similar topics

3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
3
by: SimonH | last post by:
Hi all, I would like to make a generic set of methods that could be called regardless of the database behind the scenes. One of the methods I would like would take a string sql statement and...
2
by: ljlevend | last post by:
I've noticed that in VS.NET 2.0 Beta 1 that none of the methods in System.Collections.Generic.List are overridable. In my app I currently have over 50 strongly typed ArrayLists that inherit from...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
0
by: Mikkel Blanné | last post by:
I haven't been able to find any references to using this combination of technologies (remoting + generic methods + method overloading). I don't think the problem has to do with C#, but I couldn't...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
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...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.