Reputation: 13
public struct Vector3<T>
{
private T X { get; set; }
private T Y { get; set; }
private T Z { get; set; }
public Vector3(T x, T y, T z) : this()
{
this.X = x;
this.Y = y;
this.Z = z;
}
public static Vector3<T> operator +(Vector3<T> v1, Vector3<T> v2)
{
return new Vector3<int>(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
}
}
Gives error: Cannot apply operator '+' to oparands of type 'T' and 'T'.
Please help resolve.
Upvotes: 0
Views: 116
Reputation: 62276
You can overcome that limitation on your own risk, by changing templated private members of the class to dynamic
type.
Example:
public struct Vector3<T>
{
private dynamic X { get; set; }
private dynamic Y { get; set; }
private dynamic Z { get; set; }
...
....
}
There is no way, unfortunately, in C#
to restrict template parameter to numeric values (I guess it is what you are trying to achieve).
Upvotes: 2