Is there a way to declare a generic function that the generic type is of type1 or type2?
example:
public void Foo(T number)
{
}
Use overloaded methods instead:
public void Foo(int number)
{
}
public void Foo(long number)
{
}
You cannot perform arithmetical operations on generic types anyway. Note that you can pass an int
value to a long
parameter. It will automatically be converted to long
. Having just a single method with a long
parameter could therefore be sufficient.
Older programming languages worked after the principle "There can be only one". C# allows you to have several methods with the same name in the same class, interface or struct. These methods must have a different signature. This means, that they must have a different number of parameters or parameters with different types (or both). This is called method overloading.