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).
Related
This question already has answers here:
C# declaring variables using var vs type [duplicate]
(2 answers)
Closed 2 years ago.
I was wondering if
var list = new List<t>();
exactly the same as
List<t> list = new List<t>()
Is this exactly the same or are there any differences?
Yes, this is totally the same. The compiler will internally replace the var with the actual type, inferred from right side of the expression.
In the C# 9 you can also use another type of record: List<t> list = new() and compiler will infer the type for the right part from the left one.
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.
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.
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.
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?