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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:02:22+00:00 2026-05-28T05:02:22+00:00

Suppose I have a struct with just one field: public struct Angle { public

  • 0

Suppose I have a struct with just one field:

public struct Angle
{
    public static readonly double RadiansPerDegree = Math.PI / 180;

    private readonly double _degrees;

    public Angle(double degrees)
    {
        _degrees = degrees;
    }

    public double Degrees
    {
        get { return _degrees; }
    }

    public double Radians
    {
        get { return _degrees * RadiansPerDegree; }
    }

    public static Angle FromDegrees(double value)
    {
        return new Angle(value);
    }

    public static Angle FromRadians(double value)
    {
        return new Angle(value / RadiansPerDegree);
    }
}

This works great, until I want to do stuff like this:

var alpha = Angle.FromDegrees(90);
var beta = Angle.FromDegrees(100);
var inequality = alpha > beta;
var sum = alpha + beta;
var negation = -alpha;
//etc.

So, I implemented IEquatable<in T> and IComparable<in T>, but that still didn’t enable any operators (not even ==, <, >=, etc.).

So, I started providing operator overloads.

For example:

public static Angle operator +(Angle a, Angle b)
{
    return new Angle(a._degrees + b._degrees);
}

public static Angle operator -(Angle a)
{
    return new Angle(-a._degrees);
}

public static bool operator >(Angle a, Angle b)
{
    return a._degrees > b._degrees;
}

This worked, however, when I looked at all the operators I could conceivably overload (+, -, !, ~, ++, --, true, false, +, -, *, /, %, &, |, ^, <<, >>, ==, !=, <, >, <=, >=), I started to feel like there must be a better way. After all, the struct only contains one field, and that field is a value type.

Is there some way to enable all the operators of double in one shot? Or do I really have to type out every operator I could possibly want to support by hand?

(Even if I had two or three fields, I’d still like to be able to add the operators in one batch…)

  • 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-28T05:02:23+00:00Added an answer on May 28, 2026 at 5:02 am

    The point of overloading operators is to define how to add to manipulate objects of a custom type using those operators, so if your second field was a string array, how would you expect the ++ operator to be implemented automatically? There is no sensible answer, especially since we don’t know the context of the object or it’s usage, so the answer is yes, you do have to overload the operators yourself.

    For the record, if you really do only need one field, and it’s just a double, then don’t use a struct in the first place unless you need to overload the operators to perform some other action than they do by default — it’s a clear case of over-engineering!

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

Sidebar

Related Questions

Suppose I have: struct Vehicle {...} struct Car : public Vehicle {...} string A(Vehicle
Suppose I have: struct Magic { Magic(Foo* foo); Magic(Bar* bar); }; Is there a
I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
Suppose I have a structure in C or C++, such as: struct ConfigurableElement {
Suppose I have BaseClass with public methods A and B, and I create DerivedClass
Suppose I have a struct called Node as follows: struct foo { foo *next;
Suppose I have a struct called foo_boolean that contains some boolean values: struct foo_boolean
In a given file if I have, struct A { static int a; };
Suppose I have a game written in C/C++. For simplicity, I have: struct masterStruct
Quick question... Suppose I have a C# struct that contains a single reference member.

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.