Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How can i create a common function for adding string and int in C# using generics. Below code i am getting an error like operator + cannot be used with type T. When i used dynamics i am able to get an output. Is there any possible way with out dynamics ?

<private void="" eventargs="" mode="hold"> {
// for srting
string strResult = Add<string>("Hello", "Welcome");

//for int
int result = Add<int>(10, 15);
}

private T Add<T>(T arg1, T arg2)
{

return arg1 + arg2;
}
Posted

This doesn't make a lot of sense. There are far more types that you simply cannot "add" together, like most classes and structures. This only makes sense for numbers and strings.

There really is no advantage to what you're doing as the standard numerical addition operation is more flexible than what you're trying to do and "adding" strings together is already accomplished with String.Join[^].
 
Share this answer
 
Comments
code4Better 8-May-14 20:51pm    
i want to have a method where it can take arguments and their type will be decided in run time (i will provide the arguments only which can be added together)and returns the respective type. I dont want create same function which does same functionality for different types. That was the intention to write such a method. Do you have any idea how can we accomplish that funtion into working ??
Dave Kreskowiak 8-May-14 22:39pm    
Frankly, that's a stupid idea. The only types you can add/append together are numerics and strings and those already have their respective support built in. The way you've written your code, you can only ever use a single type for both operands, making the code you're writing a moot point (see previous sentence).
code4Better 11-May-14 17:03pm    
Hello Dave,
You dont need to tell me whether it is stupid idea or not. I am already able to achieve the result using dynamic. My question was with out using dynamic. If you dont the answer you just keep quiet or just go and read something and then comment.
Dave Kreskowiak 11-May-14 17:25pm    
Have a nice life.
You do not need any generics for that. The + operator is already overloaded for string and int:
C#
int i = 0;
string str = "";
string result = i + str;
string ret = str + i;

That compiles; i.ToString() is called somewhere in the background when the values are added.
 
Share this answer
 
Comments
code4Better 8-May-14 20:46pm    
Hello Hiller,
Thanks for your time.
As you said we can add int and string together but i want to have a method where it can take arguments and their type will be decided in run time (i will provide the arguments only which can be added together)and returns the respective type. That was the intention to write such a method. Do you have any idea how can we accomplish that funtion into working ??
Bernhard Hiller 9-May-14 2:40am    
When you use Generics with a "where" clause, all the conditions of the where clause are combined with a logical AND, not with a logical OR (at least upto .Net 4) - hence it is not possible to provide a list of acceptable types for the where clause.
code4Better 11-May-14 17:05pm    
Thanks for your input Bernhard Hiller.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900