Is there a way to declare a generic function that the generic type is of type1 or type2?
example:
public void Foo<T>(T number)
{
}
Can I constraint T to be int or long
Is there a way to declare a generic function that the generic type is of type1 or type2?
example:
public void Foo<T>(T number)
{
}
Can I constraint T to be int or long
For ReferenceType objects you can do
public void DoIt<T>(T someParameter) where T : IMyType
{
}
...
public interface IMyType
{
}
public class Type1 : IMyType
{
}
public class Type2 : IMyType
{
}
For your case using long as parameter will constrain usage to longs and ints anyway.
public void DoIt(long someParameter)
{
}
to constrain to any value types (like: int, double, short, decimal) you can use:
public void DoIt<T>(T someParameter) where T : struct
{
}
for more information you can check official documentation here
Although you could use a generic constraint to limit the type of each generic argument T, unfortunately there is none that would allow you to enforce at compile time whether T is type1 or type2
.
Nor is there any way to enforce at compile time that your generic argument can only be of any primitive type (int, long, double, ...).
No.
That doesn't make sense; T
would not have any usable compile-time type in the method.
Instead, you should make two overloaded methods.
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.
I know this is an old question, and this doesn't perfectly answer it, but you can do this with a single method, rather than creating multiples, or using generic constraints... especially useful if you have 20 odd types to check.
Obviously you don't get the compiler type checking as you do when using a constraint, but this can help in certain circumstances...
public void MyMethod<T>()
{
if (!typeof(T).Equals(typeof(int)) &&
!typeof(T).Equals(typeof(long)))
throw new Exception("T must be int or long");
//your logic here
}
I also had this problem and I think I found a better solution (assuming an overloaded version of your method is insufficient):
Mixing Type1
and Type2
without any parallels does not make any sense as has been written already. So there has to be any method or property accessed for both object types. To make sure for the compiler that these methods or properties are available for your object, group Type1
and Type2
by creating an interface MyInterface
and implementing it by Type1
and Type2
:
interface MyInterface {
void MyCommonMethod();
bool MyCommonProperty { get; }
}
class Type1 : MyInterface {
void MyCommonMethod() {
// TODO: Implement it for Type1
}
bool MyCommonProperty {
get {
// TODO: Implement it for Type1
}
}
}
class Type2 : MyInterface {
void MyCommonMethod() {
// TODO: Implement it for Type2
}
bool MyCommonProperty {
get {
// TODO: Implement it for Type2
}
}
}
Now, to rewrite your Foo
method to accept both Type1
and Type2
, constraint T
to be an MyInterface
object:
public void Foo<T>(T number) where T : MyInterface
{
throw new NotImplementedException();
}
I mope this might be helpful. :)
I don't think this is currently possible.
This question about creating a math library sort of covers the same ground, and includes some work arounds.