Is it possible to have a generic Addition method [duplicate] - c#

This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
How to cast an object into its type?
Solution for overloaded operator constraint in .NET generics
Hi All,
Is it possible to have a generic Addition method?
something like:
public void Add<T>(T a, T b)
{
T c = a + b;//Error
}
Actually operands '+' can not be applied to Type T. Is there any way around?

Related

Does such an interface in c# exists: IADD<T>? [duplicate]

This question already has answers here:
Define a generic that implements the + operator [duplicate]
(2 answers)
Closed 1 year ago.
I would like to write a generic sum function on an enumerable of elements that have the + operator implemented.
public T Add<T>(IEnumerable<t> list)=> list.Aggregate((x,y)=>x+y)
I need to say that T implement the + operator with an element of the same type.
with T:IADD<T>
I didn't find if such an interface exists in c#
No. There is a proposal to add something like this, but (by the location) it is just "under consideration", and has not been earmarked for vNext (C# 10 at time of writing).

Generics with operators C# [duplicate]

This question already has answers here:
Arithmetic operator overloading for a generic class in C#
(13 answers)
How can I subtract two generic objects (T - T) in C# (Example: DateTime - DateTime)?
(6 answers)
Closed 9 years ago.
I need to use a generic method that subtracts the two operands, like this, with some pseudo coding.
public double GetResult<T>(T Arg1, T Arg2) : where T contains "-" operator
{
return Arg1 - Arg2;
//No, they are not value types, they are classes implementing the "-" operator
//And eventually, they can be value types
}
Is that possible? Like there is a where T = new() for types with creators, is there a where T contains "-" operator?
The library at http://www.yoda.arachsys.com/csharp/miscutil/ contains classes for performing math operations on generic arguments.

About Generic types with like T [duplicate]

This question already has answers here:
What does "where T : class, new()" mean?
(11 answers)
Closed 9 years ago.
I wonder what is this? This kind of a generic method I think. It has a part with 'where'. What about that? There is also generic classes I've heard. How can I learn these can you recommend an article?
protected T Item<T>() where T : class
{
return GetDataItem() as T ?? default(T);
}
The where clause is called a "generic constraint". In that case, where T: class dictates that T must be a reference type (i.e., not a struct).
More info on generic constraints: http://msdn.microsoft.com/en-us/library/d5x73970.aspx
And generic classes: http://msdn.microsoft.com/en-us/library/sz6zd40f.aspx
Edit
In the snippet you provided, the constraint is needed because otherwise the null-coalescing operator (??) wouldn't make sense, since value types (structs) can't be null.

Applying '==' operator to generic parameter [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can’t operator == be applied to generic types in C#?
I have a DatabaseLookup{} class where the parameter T will be used by the lookup methods in the class. Before lookup, I want to see if T was already looked up with something like
if (T == previousLookupObject) ...
This doesn't compile at all. What is preventing me from making a simple comparison like this?
T is the type parameter. If your previousLookupObject is an object of Type, you need to do typeof(T) == previousLookupObject.
If previousLookupObject is variable of type T, you need to have an actual object of T to compare it to.
If you want to find out if previousLookupObject is of type T, you need to use the is operator: if (previousLookupObject is T).
T is type, previousLookupObject is (I suppose) an object instance. So you are comparing apples to oranges. Try this:
if (previousLookupObject is T)
{
...
}
Refer to the following links:
Can't operator == be applied to generic types in C#?
c# compare two generic values
What type is previousLookupObject? Generic type parameters are types, and can't be used as normal object references.

Default for generic type? [duplicate]

This question already has answers here:
Is there a reasonable approach to "default" type parameters in C# Generics?
(6 answers)
Closed 9 years ago.
Is it possible to do something like
public class PriorityQueue<TValue, TPriority=int> where TPriority : IComparable
(note the =int) ?
Before you suggest it, yes, I know I can just add another line:
public class PriorityQueue<TValue> : PriorityQueue<TValue, int> { }
But I'm wondering if it's possible to do it as a param.
No. There is no option for default types on generic types in C#.
Your second example is often the "best" option available, if you need this behavior.

Categories

Resources