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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:18:22+00:00 2026-06-02T21:18:22+00:00

I have a type that represents a type of number. In this case, I’m

  • 0

I have a type that represents a type of number. In this case, I’m working with megawatts, so I have created a type called Megawatt. I’d like to be able to work with these megawatts like I would an Int, Double, or any type of c# number type. Is this possible? If so, how do I do it?

Example:

public class Megawatt{
    public double Value { get; set; }
}

I want to be able to do this:

var startOfDay = new Megawatts{value=100};
var endOfDay = new Megawatts{value=65};
Megawatt result = startOfDay - endOfDay;

This is possible with DateTime… you can subtract one DateTime from another and get a TimeSpan. I’m hoping to do something similar.

  • 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-02T21:18:27+00:00Added an answer on June 2, 2026 at 9:18 pm

    In addition to the good answers posted so far: you should make your type an immutable struct rather than a mutable value type. This is precisely the sort of job that immutable value types were designed for.

    struct Megawatt
    {
        private double Value { get; private set; }
        public Megawatt(double value) : this()
        {
            this.Value = value;
        }
        public static Megawatt operator +(Megawatt x, Megawatt y)
        {
            return new Megawatt(x.Value + y.Value);
        }
        public static Megawatt operator -(Megawatt x, Megawatt y)
        {
            return new Megawatt(x.Value - y.Value);
        }
        // unary minus
        public static Megawatt operator -(Megawatt x)
        {
            return new Megawatt(-x.Value);
        }
        public static Megawatt operator *(Megawatt x, double y)
        {
            return new Megawatt(x.Value * y);
        }
        public static Megawatt operator *(double x, Megawatt y)
        {
            return new Megawatt(x * y.Value);
        }
    }
    

    And so on. Note that you can add together two megawatts, but you cannot multiply two megawatts; you can only multiply megawatts by doubles.

    You could also add more unit-laden types. For example, you could create a type MegawattHour and a type Hour and then say that Megawatt times Hour gives MegawattHour. You could also say that there is another type Joule, and there is an implicit conversion from MegawattHour to Joule.

    There are a number of programming languages that support these sorts of operations with less verbosity than C#; if you do a lot of this sort of thing you might look into F#.

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

Sidebar

Related Questions

I have generic type that looks like: public class GenericClass<T, U> where T :
I have a table defined like this: Column: Version Message Type: varchar(20) varchar(100) ----------------------------------
I have to control input from 1 to 9999999999 (that represents the registration number
I have a 2D array that holds unique integers - this represents a physical
I have this table that represents a weak entity and is a typical table
Here briefly are the business requirements. I have an entity called PricingSchedule that represents
I think that this is a newbie type question but I have quite understood
I have a written C++ server app that I would like to be able
I currently have a type that I inject into my controllers that's used for
I have a data type that contains a set and a method that expects

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.