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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:15:55+00:00 2026-05-30T01:15:55+00:00

Possible Duplicate: C# generic constraint for only integers As you can see in the

  • 0

Possible Duplicate:
C# generic constraint for only integers

As you can see in the following code, I need to compute the sum of two generic numbers.

public class NumberContainer<T>
{
    public T ValueA { get; private set; }
    public T ValueB { get; private set; }
    public T Total { get { return ValueA + ValueB; } }
}

However, it isn’t possible to do a direct addition of the two T values, which results in the compiler error below :

Operator ‘+’ cannot be applied to operands of type ‘T’ and ‘T’

Given that I don’t intend to use T for anything else than value-types that represent numbers (short, ushort, int, uint, etc), how could I perform the sum? (efficiency is a factor to be considered)

  • 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-30T01:15:57+00:00Added an answer on May 30, 2026 at 1:15 am

    You can do it with “little magic” from LINQ:

    private static readonly Func<T, T, T> adder;
    static NumberContainer() {
        var p1 = Expression.Parameter(typeof (T));
        var p2 = Expression.Parameter(typeof (T));
        adder = (Func<T, T, T>)Expression
            .Lambda(Expression.Add(p1, p2), p1, p2)
            .Compile();
    } 
    public T Total { get { return adder(ValueA, ValueB); } }
    

    The only drawback is that this code will compile even if NumberContainer is instantiated with a type T that does not support addition; of course it will throw an exception at run-time. An added benefit is that this should work with user-defined + operators.

    • 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: Why won't this generic java code compile? Given the following code: import
Possible Duplicates: C# generic constraint for only integers Generics - where T is a
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've coded
Possible Duplicate: What’s the reason I can’t create generic array types in Java? HashSet<Integer>[]
Possible Duplicate: How can I search a generic TList for a record with a
Possible Duplicate: Why can’t I catch a generic exception in C#? I have been
Possible Duplicate: Generic methods and multiple constraints I need a generic function that has
Possible Duplicate: Can’t operator == be applied to generic types in C#? I have

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.