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.
Related
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).
This question already has answers here:
explicit conversion operator error when converting generic lists
(2 answers)
Closed 6 years ago.
I add this operator to my class and works well when I pass a class of "A" it convert to class "B".
public static explicit operator B (A a)
{
//Convert A to B
}
but when I want to convert a list of "A" to a List of "B" it doesn't work.
and I try below code but it doesn't work too.
public static explicit operator List<B>(List<A> a)
{
//Convert List<A> to List<B>
}
It throw compiler error "User-defined conversion must convert to or from the enclosing type"
I don't want to use extention method to cast it
You can't use Conversion Operators for converting list of one type to another.
C# enables programmers to declare conversions on classes or structs so
that classes or structs can be converted to and/or from other classes
or structs, or basic types.
As you see, the purpose is to convert one type to another, not the list of that types.
You can use Select method instead of that:
List<B> listB = listA.Select(a => (B)a).ToList();
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?