© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
14 replies
triplemocha

❔ operator * cannot be applied to operands of type T and T

I'm getting this error:

operator * cannot be applied to operands of type T and T
operator * cannot be applied to operands of type T and T

I understand why, but I'm wondering if there's a way to add a constraint only to ints , floats and doubles. Sounds like it's a no. What else can be done?

    struct Vector2D<T>  
    {
        public T x, y;

        public Vector2D()
        {
            x = y = default(T);
        }
         
        public Vector2D(T xPos, T yPos)
        {
            x = xPos;
            y = yPos;
        }

        public T GetMagnitude()
        {
            return System.Math.Sqrt(x * x + y * y); // error
        }
    }
    struct Vector2D<T>  
    {
        public T x, y;

        public Vector2D()
        {
            x = y = default(T);
        }
         
        public Vector2D(T xPos, T yPos)
        {
            x = xPos;
            y = yPos;
        }

        public T GetMagnitude()
        {
            return System.Math.Sqrt(x * x + y * y); // error
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Operator ! cannot be applied to type string
C#CC# / help
3y ago
The instance of entity type 'OptimizerInstanceEntity' cannot be tracked
C#CC# / help
2y ago
Type cannot be found (CS7069) [Answered]
C#CC# / help
4y ago
Unable to cast object of type - tried to explicit conversion operator
C#CC# / help
3y ago