Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics - where T is a number? [duplicate]

I'm trying to figure a way to create a generic class for number types only, for doing some calculations.

Is there a common interface for all number types (int, double, float...) that I'm missing???

If not, what will be the best way to create such a class?

UPDATE:

The main thing I'm trying to achieve is checking who is the bigger between two variables of type T.

like image 402
CD.. Avatar asked Aug 12 '09 18:08

CD..


People also ask

Where t in generic C#?

The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.

How do you determine what type a generic parameter T is?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

What is a generic number?

The Generic Number or Generic Address parameter is located in the ISUP messaging and is offered to callers who wish to use a presentation/display number which is different than their main number.

What is generic in C sharp?

Generics allow you to define the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.


2 Answers

What version of .NET are you using? If you are using .NET 3.5, then I have a generic operators implementation in MiscUtil (free etc).

This has methods like T Add<T>(T x, T y), and other variants for arithmetic on different types (like DateTime + TimeSpan).

Additionally, this works for all the inbuilt, lifted and bespoke operators, and caches the delegate for performance.

Some additional background on why this is tricky is here.

You may also want to know that dynamic (4.0) sort-of solves this issue indirectly too - i.e.

dynamic x = ..., y = ... dynamic result = x + y; // does what you expect 

Re the comment about < / > - you don't actually need operators for this; you just need:

T x = ..., T y = ... int c = Comparer<T>.Default.Compare(x,y); if(c < 0) {     // x < y } else if (c > 0) {      // x > y } 
like image 52
Marc Gravell Avatar answered Sep 17 '22 20:09

Marc Gravell


There are interfaces for some of the operations on the number types, like the IComparable<T>, IConvertible and IEquatable<T> interfaces. You can specify that to get a specific functionality:

public class MaxFinder<T> where T : IComparable<T> {     public T FindMax(IEnumerable<T> items) {       T result = default(T);       bool first = true;       foreach (T item in items) {          if (first) {             result = item;             first = false;          } else {             if (item.CompareTo(result) > 0) {                result = item;             }          }       }       return result;    }  } 

You can use delegates to expand a class with type specific operations:

public class Adder<T> {     public delegate T AddDelegate(T item1, T item2);     public T AddAll(IEnumerable<T> items, AddDelegate add) {       T result = default(T);       foreach (T item in items) {          result = add(result, item);       }       return result;    }  } 

Usage:

Adder<int> adder = new Adder<int>(); int[] list = { 1, 2, 3 }; int sum = adder.AddAll(list, delegate(int x, int y) { return x + y; }); 

You can also store delegates in the class, and have different factory methods that sets up delegates for a specific data type. That way the type specific code is only in the factory methods.

like image 39
Guffa Avatar answered Sep 21 '22 20:09

Guffa