Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading assignment operator in C#

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?

like image 641
Carson Myers Avatar asked Dec 27 '10 09:12

Carson Myers


People also ask

How do you write an assignment overloaded operator?

Overloading the assignment operator (operator=) is fairly straightforward, with one specific caveat that we'll get to. The assignment operator must be overloaded as a member function. This will call f1. operator=(f1), and under the simplistic implementation above, all of the members will be assigned to themselves.

What is overloading operator in C?

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.

What is operator overloading with example?

In C++, we can change the way operators work for user-defined types like objects and structures. This is known as operator overloading. For example, Suppose we have created three objects c1 , c2 and result from a class named Complex that represents complex numbers.


2 Answers

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.)

like image 154
Jon Skeet Avatar answered Oct 07 '22 06:10

Jon Skeet


You can create implicit conversion operators. There is a page on MSDN with a nice example.

It's also a good idea to make them immutable structs. That's exactly what the "primitives" are, and that's what makes it impossible to inherit from them. You want a struct because you want value-type semantics, instead of reference type semantics. And you want them immutable because mutable value types are generally a bad idea.

like image 34
R. Martinho Fernandes Avatar answered Oct 07 '22 05:10

R. Martinho Fernandes