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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:13:29+00:00 2026-06-05T13:13:29+00:00

Is it possible to implement basic arithmetic (at least addition) in C# generics, like

  • 0

Is it possible to implement basic arithmetic (at least addition) in C# generics, like you can with C++ templates? I’ve been trying for a while to get them up and working, but C# doesn’t let you declare the same generic type multiple times, like you can with templates.

Extensive googling did not provide an answer.

EDIT: Thanks, but what I’m looking for is a way to do the arithmetic at compile time, embedding something like Church numerals in generics types. That’s why I linked the article that I did. Arithmetic in generic types, not arithmetic on instances of generic types.

  • 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-06-05T13:13:33+00:00Added an answer on June 5, 2026 at 1:13 pm

    Unfortunately you cannot use arithmetic operations on generic types

    T Add(T a, T b)
    {
        return a + b; // compiler error here
    }
    

    will not work in c#!

    But you can create your own numeric types and overload the operators (arithmetic, equality and implicit, explicit). This lets you work with them in a quite natural way. However you cannot create an inheritance hierarchy with generics. You will have to use a non generic base class or interface.

    I just did it with a vector type. A shortened version here:

    public class Vector
    {
        private const double Eps = 1e-7;
    
        public Vector(double x, double y)
        {
            _x = x;
            _y = y;
        }
    
        private double _x;
        public double X
        {
            get { return _x; }
        }
    
        private double _y;
        public double Y
        {
            get { return _y; }
        }
    
        public static Vector operator +(Vector a, Vector b)
        {
            return new Vector(a._x + b._x, a._y + b._y);
        }
    
        public static Vector operator *(double d, Vector v)
        {
            return new Vector(d * v._x, d * v._y);
        }
    
        public static bool operator ==(Vector a, Vector b)
        {
            if (ReferenceEquals(a, null)) {
                return ReferenceEquals(b, null);
            }
            if (ReferenceEquals(b, null)) {
                return false;
            }
            return Math.Abs(a._x - b._x) < Eps && Math.Abs(a._y - b._y) < Eps;
        }
    
        public static bool operator !=(Vector a, Vector b)
        {
            return !(a == b);
        }
    
        public static implicit operator Vector(double[] point)
        {
            return new Vector(point[0], point[1]);
        }
    
        public static implicit operator Vector(PointF point)
        {
            return new Vector(point.X, point.Y);
        }
    
        public override int GetHashCode()
        {
            return _x.GetHashCode() ^ _y.GetHashCode();
        }
    
        public override bool Equals(object obj)
        {
            var other = obj as Vector;
            return other != null && Math.Abs(other._x - _x) < Eps && Math.Abs(other._y - _y) < Eps;
        }
    
        public override string ToString()
        {
            return String.Format("Vector({0:0.0000}, {1:0.0000})", _x, _y);
        }
    }
    

    More than 10 years have past since I answered this question and finally the C# 11 feature – static virtual members in interfaces was introduced in .NET 7 allowing us to do Generic math and many other things.

    Interfaces can now declare static factory methods not limiting us to use parameter-less constructors in generic contexts.

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

Sidebar

Related Questions

While trying to learn c++, I tried to implement class representing very basic trie.
How can I implement basic auth using php? Is it possible? Thank you
It's possible to implement INotifyCollectionChanged or other interface like IObservable to enable to bind
For an assignment, we had to implement something like a very basic sexp parser,
Possible Duplicate: How would you implement a basic event-loop? Not really a language-specific question.
I would like to implement my own basic Runge-Kutta 4 integrator in Python. The
A basic functionality I am trying to implement in UITextView . I have a
I'm trying to implement a basic MIME parser for the multipart/related in C++/Qt. So
is it possible to implement SignalR without the use of Jquery. I want to
Is it possible to implement a gesture recognizer in a view and propagate it

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.