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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:03:17+00:00 2026-05-18T22:03:17+00:00

I know the = operator can’t be overloaded, but there must be a way

  • 0

I know the = operator can’t be overloaded, but there must be a way to do what I want here:

I’m just creating classes to represent quantitative units, since I’m doing a bit of physics. Apparently I can’t just inherit from a primitive, but I want my classes to behave exactly like primitives — I just want them typed differently.

So I’d be able to go,

Velocity ms = 0;
ms = 17.4;
ms += 9.8;

etc.

I’m not sure how to do this. I figured I’d just write some classes like so:

class Power
{
    private Double Value { get; set; }

    //operator overloads for +, -, /, *, =, etc
}

But apparently I can’t overload the assignment operator. Is there any way I can get this behavior?

  • 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-18T22:03:17+00:00Added an answer on May 18, 2026 at 10:03 pm

    It sounds like you should be using a struct rather than a class… and then creating an implicit conversion operator, as well as various operators for addition etc.

    Here’s some sample code:

    public struct Velocity
    {
        private readonly double value;
    
        public Velocity(double value)
        {
            this.value = value;
        }
    
        public static implicit operator Velocity(double value)
        {
            return new Velocity(value);
        }
    
        public static Velocity operator +(Velocity first, Velocity second)
        {
            return new Velocity(first.value + second.value);
        }
    
        public static Velocity operator -(Velocity first, Velocity second)
        {
            return new Velocity(first.value - second.value);
        }
    
        // TODO: Overload == and !=, implement IEquatable<T>, override
        // Equals(object), GetHashCode and ToStrin
    }
    
    class Test
    {
        static void Main()
        {
            Velocity ms = 0;
            ms = 17.4;
            // The statement below will perform a conversion of 9.8 to Velocity,
            // then call +(Velocity, Velocity)
            ms += 9.8;
        }
    }
    

    (As a side-note… I don’t see how this really represents a velocity, as surely that needs a direction as well as a magnitude.)

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

Sidebar

Related Questions

I know that you can use a dummy int parameter on operator++ and operator--
I know the standard way of using the null coalescing operator in C# is
I know it's ridiculous, but I need it for storage optimization. Is there any
I am trying to write operator overload for custom class and don't know how
My coworker is new to C# and didn't know about the coalesce operator. So,
Is there any way to determine the type of a variable passed as an
In Python, the assignment operator can unpack a list or a tuple into variables,
1) According to my book, is operator can check whether expression E ( E
I've long had a dream of creating a consistent comparison operator to use in
I'm a bit stuck investigating the how-to's of sending operator commands (dont know the

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.