Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3979660
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:11:49+00:00 2026-05-20T05:11:49+00:00

After coming up against this problem myself in trying to implement a generic Vector2<int/float/double>

  • 0

After coming up against this problem myself in trying to implement a generic Vector2<int/float/double> in C#, I’ve done a bunch of investigation into this problem, also described in this question:

Less generic generics? A possible solution for arithmetic in C# generics

These links contain some more background information and fascinating solution approaches:

https://jonskeet.uk/csharp/miscutil/usage/genericoperators.html

http://www.codeproject.com/KB/cs/genericnumerics.aspx

Now that C# 4.0 is out with its new versatile dynamic type, my question for the brilliant SO community, is this: is it a tool that could be used perhaps to build performant, generic Vector/Matrix/etc. numeric types?

Clearly a Vector2 could be built by simply like so:

public struct Vector2
{
    public dynamic X;
    public dynamic Y;

    public Vector2(dynamic x, dynamic y)
    {
        this.X = x;
        this.Y = y;
    }

    public static Vector2 operator+(Vector2 a, Vector2 b)
    {
        return new Vector2(a.X + b.X, a.Y + b.Y);
    }
}

but with this approach we have no type constraint here, so you could make a Vector2(3, 12.4572). Is there a way that we could mix dynamic members with a type parameter Vector2<int> to perform our math operations as would be done with ints?

Perhaps some form of casting could be used to ensure this.X is a T, though I don’t know how that would perform.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:11:50+00:00Added an answer on May 20, 2026 at 5:11 am

    Only you can tell if dynamic operator invocations will meet your performance requirements, but it’s certainly possible to address some of your type-safety concerns with generics – there’s no reason that everything has to be checked at run-time just because of one little dynamic call:

    // Consider making this type immutable
    public struct Vector2<T>
    {
        public T X;
        public T Y;
    
        public Vector2(T x, T y)
        {
            this.X = x;
            this.Y = y;
        }
    
        //  The only dangerous operation on the type
        public static Vector2<T> operator +(Vector2<T> a, Vector2<T> b)
        {
            return new Vector2<T>((dynamic)a.X + b.X, (dynamic)a.Y + b.Y);
        }
    }
    

    Now, the only dangerous operation is to actually add 2 vectors of the same type (the addition operator needs to work as expected on the type-argument), but everything else is perfectly type-safe, as it should be. You can’t do new Vector<int>("a", 5), add a Vector<int> and a Vector<string>, or assign the addition of two Vector<int>s to a Vector<string>. Note that none of these errors would have been caught at compile-time with your original solution.

    Note that:

    1. There’s nothing to stop you from using generics here but going down the compiling-an-expression-tree route for the addition instead of dynamic. Delegate invocations aren’t free, but they should in theory be faster than the dynamic approach in this case – at the very least, you avoid boxing value-types. Only you can tell if they will be fast enough, though.

    2. In all cases, consider writing a static-constructor that validates that the type-argument in fact has a suitable addition operator, so that type-errors happen early on in the game.


    EDIT (OP isn’t satisfied with the performance of dynamic here):

    The expression-tree approach would look something like:

    public struct Vector2<T>
    {
        private static readonly Func<T, T, T> Add;
    
        // Create and cache adder delegate in the static constructor.
        // Will throw a TypeInitializationException
        // if you can't add Ts or if T + T != T 
        static Vector2()
        {
            var firstOperand = Expression.Parameter(typeof(T), "x");
            var secondOperand = Expression.Parameter(typeof(T), "y");
            var body = Expression.Add(firstOperand, secondOperand);
            Add = Expression.Lambda<Func<T, T, T>>
                  (body, firstOperand, secondOperand).Compile();
        }
    
        public T X;
        public T Y;
    
        public Vector2(T x, T y)
        {
            this.X = x;
            this.Y = y;
        }
    
        public static Vector2<T> operator +(Vector2<T> a, Vector2<T> b)
        {
            // Delegate invocation instead of dynamic operator invocation.
            return new Vector2<T>(Add(a.X, b.X), Add(a.Y, b.Y));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm coming up against an unexpected daylight savings time problem in code I thought
i am coming back because i still have problem using JodaTime. After the previous
I'm finding myself doing more C/C++ code against Win32 lately, and coming from a
It's been a while since I programmed in C++, and after coming from python,
coming back to postgresql after several years of oracle ... what are the state-of-the
After reading this question , I was reminded of when I was taught Java
I'm having a lot of trouble with Ruby after coming back to it from
After scouring the net for answers, coming up with almost solutions... I decided to
I am (as most ) coming from a mySQL background trying to switch over
How can I set the content inset on a UITableView after coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.