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

  • Home
  • SEARCH
  • 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 6140961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:11:05+00:00 2026-05-23T18:11:05+00:00

I am working in a small educational project where we have to implement a

  • 0

I am working in a small educational project where we have to implement a n-dimensional matrix. Depending on the context, this matrix has either to work with our own built-in ComplexNumber struct or with System.Double and for very simple examples, with integral types (mainly System.Int32).

Due to the nature of the application, we are not demanded to implement lightning fast performance.

My first idea was therefore to implement a Matrix<T> where T would somehow need to be restricted to “numbers”.

The obvious problem in doing this is that there is no way right now in the language to constraint the generic type T with defined operators. Also I do not see an easy way to restrcit T to reasonable types.

My questions are:

  1. Can somebody please point me in the direction of an elegant way to make mathematical operations with generic types that doesn’t compromise performance too much and to somehow make that work with built in types (if possible).

  2. If Eric ever reads this, does this feature (constraining generic types by defined operators) ever come up in hypothetical future versions of C# design meetings and has it ever been close to making it into the language?

I know its easier and better to implement a ComplexMatrix type and create wrappers around it for each matrix “sub-type” (double, integral, etc.) and pay the performance costs of the conversions between our complex type and whichever type the matrix elements happen to be. This question is more out of curiousity on how would someone implement a similar scenario.

  • 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-23T18:11:05+00:00Added an answer on May 23, 2026 at 6:11 pm

    If Eric ever reads this,

    If you want something brought to my attention, try the contact link on my blog. Or put my full name in the text of the question so that me searching for myself will find me.

    does this feature (constraining generic types by defined operators) ever come up in hypothetical future versions of C# design meetings and has it ever been close to making it into the language?

    Indeed, this is a frequently requested feature. We’ve been getting requests for this sort of thing since C# 1.0.

    The feature would requires support from the CLR team, not just the language — it is the sort of feature that we would want to integrate into all our languages, which increases the cost.

    The CLR team has expressed interest in features like this, but they also have a lot of competing features that they could be doing, and limited time and effort to implement those features.

    There are numerous ways such a feature could be implemented. For example, we could add the ability to specify static methods in interfaces:

    interface IAddable<T>
    {
        static T operator+(T x, T y);
    }
    

    and then

    static T Sum<T>(IEnumerable<T> seq) where T : IAddable<T>
    {
        T sum = default(T);
        foreach(T item in seq) sum = sum + item;
        return sum;
    }    
    

    The idea would be that the interface means “a type that implements this interface must have the given static methods”. We’d then make int automatically implement IAddable<int>, and so on.

    How do do so efficiently in a world with runtime-generated generic code is an open question.

    I hasten to add that this is just a sketch of an idea. There are many ways to implement this sort of feature. The “statics in interfaces” idea is one that has broader usage than just mathematics, and that’s attractive to us. If we’re going to go to the huge expense of this sort of feature, it would be nice to have a really general, powerful feature rather than one narrowly focussed on math.

    On the other hand, the perfect is the enemy of the good; it might be better to just concentrate on the math problem and not go for a more expensive general solution.

    It’s an ongoing debate. It is definitely on everyone’s radar screen, but I would not expect it any time soon. The language designers are all heads-down working on going through the feedback from the async CTP.

    As always, Eric’s musings about hypothetical future language features of hypothetical unannounced future products are for entertainment purposes only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a small UML editor project, in Java, that I started a
I'm working on a small project to help me tag content on Flickr. I've
I'm working on some small chat application. I want to implement smilies over there
I'm working on a small distributed computing project. The server can't handle the calculations
I am working on a small project which requires me to load images into
I've been quite used to working on small projects which I coded with 1,000
I'm working on a small templating engine, and I'm using DOMDocument to parse the
I am working on a small intranet site for a small company, where user
I am working on a small application in VB.NET. The program needs administrator privilege
I'm working with a small FAT16 filesystem, and I want to generate CRC values

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.