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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:16:19+00:00 2026-05-16T16:16:19+00:00

Possible Duplicate: Solution for overloaded operator constraint in .NET generics I have a problem

  • 0

Possible Duplicate:
Solution for overloaded operator constraint in .NET generics

I have a problem I’m working on, currently it is working for ints but i want it to work for all classes that can be added using the + operator. Is there any way to define this in the generic? For example,

public List<T> Foo<T>() where T : ISummable

Is there any way to do this?

EDIT:
Performance of passing in a delegate to do the summing instead of using += with a type of Int was 540% slower at best. Investigating possible other solution

Final solution:
Thank you all for your suggestions. I ended up with a solution which is not too slow and enforces the checking at compile time. I cannot take full credit as a colleague helped me arrive at this. Anyway, here it is:

Implement an interface with all of the required operators on it in the form of functions

public interface IFoo<InputType, OutputType>
{
    //Adds A to B and returns a value of type OutputType
    OutputType Add(InputType a, InputType b);
    //Subtracts A from B and returns a value of type OutputType
    OutputType Subtract(InputType a, InputType b);
}

Create the class you want to define, but instead of the Where clause, use a dependency injected instance of the IFoo interface. the OutputType will most often be double because the nature of the operations is mathematical.

public class Bar<T>
{
    private readonly IFoo<T,double> _operators;

    public Bar(IFoo<T, double> operators)
    {
        _operators = operators;
    }
}

now when you use this class, you define the rules for operation like this:

private class Foo : IFoo<int, double>
{
    public double Add(int a, int b)
    {
        return (double)(a+b);
    }
    public double Subtract(int a, int b)
    {
        return (double)(a-b);
    }
}

then you would use it like this:

Foo inttoDoubleOperations = new Foo();
Bar myClass = new Bar(Foo);

that way all the operations are enforced at compile time 🙂

enjoy!

  • 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-16T16:16:19+00:00Added an answer on May 16, 2026 at 4:16 pm

    This is a pretty commonly requested new feature for C#: the ability to specify more generic parameter constraints than the ones we already have. Operators are among the most frequently asked. However, C# does not currently support this.

    Possible workarounds:

    • Pass a delegate to any method that needs to do addition. This is the most type-safe option, but of course it’s annoying if you need to call such a method often. For example:

      public class Generic<T> {
          public void DoSomething(T anItem, T anotherItem, Func<T, T, T> add) {
              // instead of
              Blah(anItem + anotherItem);
              // have to write:
              Blah(add(anItem, anotherItem));
          }
      }
      
      Generic<int> genInt = ...;
      // and then instead of ...
      genInt.DoSomething(1, 2);
      // have to write:
      genInt.DoSomething(1, 2, (a, b) => a + b);
      
    • Declare your own interface IAddable. Then you can use it as a generic type parameter constraint, but obviously you can’t use int as the parameter then. You would have to use a struct of your own that contains just an int and which implements IAddable:

      public interface IAddable<T> {
          T Add(T other);
      }
       
      public struct Integer : IAddable<Integer> {
          public int Value;
          public Integer(int value) { Value = value; }
          public Integer Add(Integer other) { return new Integer(Value + other.Value); }
      }
      
      // then instead of
      Generic<int> blah = ...;
      // have to write:
      Generic<Integer> blah = ...;
      
    • dynamic. Another possible workaround is to use dynamic, but this is rather hacky and completely unsafe: it will let you pass in any type and call any method or operator, and only crash at runtime, not at compile-time.

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

Sidebar

Related Questions

Possible Duplicate: Project Euler, Problem 10 java solution not working So, I'm attempting to
Possible Duplicate: How costly is .NET reflection? The elegant solution to a problem I
Possible Duplicate: Java: recommended solution for deep cloning/copying an instance I have an object
Possible Duplicate: Solution to a recursive problem (code kata) give an algorithm to find
Possible Duplicate: Copy to Output Directory copies folder structure but only want to copy
Possible Duplicate: Force Download an Image Using Javascript Basically I want to have the
Possible Duplicate: Cannot connect to UserPC\SQLExpress I have been struggling to find a solution
Possible Duplicate: Elegant solution to duplicate, const and non-const, getters? Say I have a
Possible Duplicate: Strange javascript addition problem I know there's obviously a solution for this,
Possible Duplicate: Group files in Visual Studio The problem is, that I want to

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.