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 6101269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:28:50+00:00 2026-05-23T13:28:50+00:00

Possible Duplicates: C# generic constraint for only integers Generics – where T is a

  • 0

Possible Duplicates:
C# generic constraint for only integers
Generics – where T is a number?

In c#:

Imagine a case that we have a functionality over a generic type in which conceptually our generic type is limited only to numbers.

By saying “number” we aim to be able to use multiply, plus, minus, etc. operators on variables of type T.

Unfortunately something like this is not accepted in c#:

public class Image<T> where T : number

Note that performance is also important so we don’t want to go for redefining a struct for numeric types and using them.

What do you think is the best way to do this? Or is there any design patter that allows us to have “high performance” arithmetic functions over variables of a generic type?

  • 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-23T13:28:51+00:00Added an answer on May 23, 2026 at 1:28 pm

    At the great cost of losing readability, you can rewrite all methods from

    int Factorial(int x)
    {
        if (x == 0) return 1;
        return x * Factorial(x - 1);
    }
    

    to

    T Factorial<T>(IReal<T> t, T x)
    {
        if (t.Equals(x, t.FromInteger(0))) return t.FromInteger(1);
        return t.Multiply(x, Factorial<T>(t, t.Subtract(x, t.FromInteger(1))));
    }
    

    using

    interface IEq<T>
    {
        bool Equals(T a, T b);
    }
    
    interface IOrd<T> : IEq<T>
    {
        int Compare(T a, T b);
    }
    
    interface IShow<T>
    {
        string Show(T a);
    }
    
    interface INum<T> : IEq<T>, IShow<T>
    {
        T Add(T a, T b);
        T Subtract(T a, T b);
        T Multiply(T a, T b);
    
        T Negate(T a);
    
        T Abs(T a);
        T Signum(T a);
    
        T FromInteger(int x);
    }
    
    interface IReal<T> : INum<T>, IOrd<T>
    {
    }
    

    with implementations for any number type.

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

Sidebar

Related Questions

Possible Duplicate: C# generic constraint for only integers Greets! I'm attempting to set up
Possible Duplicate: Solution for overloaded operator constraint in .NET generics I have a problem
Possible Duplicate: Making a generic property I'm not quite sure how to do that,
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Possible Duplicates: Exception during iteration on collection and remove items from that collection How
Possible Duplicates: is “else if” faster than “switch() case” ? What is the relative
Possible Duplicate: Java how to: Generic Array creation Error generic array creation I have
Possible Duplicate: Generic methods and multiple constraints I need a generic function that has
Possible Duplicate: How to determine the class of a generic type? I have a
I currently have a couple different user controls that provide the same functionality: three

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.