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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:31:44+00:00 2026-05-16T20:31:44+00:00

Possible Duplicate: Define a generic that implements the + operator I am recently working

  • 0

Possible Duplicate:
Define a generic that implements the + operator

I am recently working on a C# class library implementing an algorithm. The point is that I would like the users of the library to be able to choose the machine precision (single or double) the algorithm should operate with, and I’m trying to do it with generics. So, for instance:

    Algorithm<double> a = new Algorithm<double>();
    /** Some initializations here */
    double result = a.Solve();

or

    Algorithm<float> a = new Algorithm<float>();
    /** Some initializations here */
    float result = a.Solve();

Thus, the type parameter for the generic classes is meant to be a decimal number (because in the algorithm code I need to use +, *, /, -), but I don’t know which kind of type constraint to impose on it. I have thought about building an interface with all the operators but, unfortunately, this is not allowed. Any ideas?

Otherwise, is it possible to obtain in C# something similar to template specialization in C++?

Thank you

Tommaso

  • 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-16T20:31:44+00:00Added an answer on May 16, 2026 at 8:31 pm

    You can’t, basically. The CLR type system doesn’t support that kind of constraint.

    I’ve previous blogged about how we could describe such constraints using “static interfaces” but I don’t know of any plans to do anything similar.

    You might want to look at the MiscUtil generic operator support which would let you use the operators – but you’d have to use a constraint of just struct or something like that, which would of course allow things like Guid which you don’t want.

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

Sidebar

Related Questions

Possible Duplicate: Making a generic property I'm not quite sure how to do that,
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic This
I have an abstract generic view-model that I use as a base-class for several
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java
Possible Duplicate: static const vs #define in C I started to learn C and
Possible Duplicate: Why use enum when #define is just as efficient? When programming in
Possible Duplicate: Is there a reason you can not define the access modifier on
Possible Duplicate: Problem with Macros Hi all I have defined this macro: #define SQ(a)

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.