Override operator c# - c#

I would like to override operator * for my class. I know how to handle * for MyClass * MyClass but how can i implement this for having possibility to multiply for example:
7 * MyClass
7 is lets say double and MyClass is object of MyClass,
thanks for help

When you provide an operator for your class, the parameter types for the operator implementation control which situations you're handling.
To illustrate:
// Declare which operator to overload (+), the types
// that can be added (two Complex objects), and the
// return type (Complex):
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
This method defines what to do if you want to add two Complex numbers.
If you tried to use this with (say) a double and a Complex, compilation would fail.
In your case, you need just declare both versions:
public static MyClass operator *(MyClass c1, MyClass c2)
{
return ...
}
public static MyClass operator *(double n, MyClass c)
{
return ...
}

The appropriate signature in this case is:
public static MyClass operator *(double a, MyClass b)
This means it takes a double as the left side, a MyClass as the right, and returns a MyClass.

Related

Defining operators for specific instances of a generic

I am attempting to implement the generic Vector3 struct, and havethe operators for my struct that allows basic math when the type T is a numeric (int, float, double, long, short)
I had thought the way to do this was to just define the 4 basic operators for all each something like
public static Vector3<int> operator +(Vector3<int> left, Vector3<int> right)
but that gives me the error that at least one of the parameters must be of the containing type (which is Vector3 in this case)
I feel reasonably confident there is a way for me to define a Vector3 generic, and still have the convenience of the standard operators, but I can not seem to figure out what I need to write syntactically.
I believe you are trying to do something like this:
public class Vector3<T>
{
T x; T y; T z;
public static Vector3<int> operator + (Vector3<int> lhs, Vector3<int> rhs)
{
//Stuff
}
}
This is not allowed. Why not? Well imagine you wrote a method like this:
public static void Foo<T>()
{
var lhs = new Vector3<T>();
var rhs = new Vector3<T>();
var result = lhs + rhs;
}
Should the compiler allow this to compile or not? Because this would work:
Foo<int>();
But this would fail:
Foo<string>();
Because the compiler can't guarantee it'll work, it's not allowed.
If you have a burning desire to implement operator overloading for certain types of Vector3, you have to subclass it:
public class Vector3Int : Vector3<int>
{
public static Vector3Int operator + (Vector3Int lhs, Vector3Int rhs)
{
//Stuff
}
}
That would work. Note that I had to change struct to class as you can't inherit a struct.

User-defined implicit conversion of overloaded operator's arguments

The following operator overload is defined inside my Term class:
public static Term operator *(int c, Term t) {...}
This class also defines an implicit conversion from a Variable to Term:
public static implicit operator Term(Variable var) {...}
I would like to understand why the following does not compile:
static void Main(string[] args)
{
Variable var = ...; // the details don't matter
Console.WriteLine(2 * var); // var isn't implicitly converted to Term...
Console.ReadKey();
}
The compiler says:
Operator '*' cannot be applied to operands of type 'int' and 'OOSnake.Variable'
Why isn't my overload of operator * found?
EDIT: As per the suggestion in the comments, here is a small complete example that re-produces the error:
namespace Temp
{
class A {
}
class B
{
public static implicit operator B(A a) { return new B(); }
public static B operator *(int c, B b) { return new B(); }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(2 * new A());
}
}
}
Basically, operator overload resolution doesn't include implicit user-defined conversions in order to find the operators that could be applicable.
From section 7.3.4 of the C# 5 specification:
An operation of the form x op y, where op is an overloadable binary operator, x is an expression of type X, and y is an expression of type Y, is processed as follows:
The set of candidate user-defined operators provided by X and Y for the operation operator op(x, y) is determined. The set consists of the union of the candidate operators provided by X and the candidate operators provided by Y, each determined using the rules of ยง7.3.5. If X and Y are the same type, or if X and Y are derived from a common base type, then shared candidate operators only occur in the combined set once.
And 7.3.5 doesn't include implicit user-defined conversions in its search for a set of operators.
Note that this also wouldn't work if the implicit conversion to Term was declared in the Variable class - although that would be more reasonable to specify and implement, as the compiler could look at the set of conversions from the operand type to other types, and use those for overload resolution.
However, this is only a matter of looking for the operators to start with. The compiler is happy to perform implicit conversions when it considers whether or not an overload is applicable. For example, in your case, if you add:
class A
{
public static B operator *(A a, B b) { return new B(); }
}
Then this is valid:
A a = new A();
Console.WriteLine(a * a);
Console.WriteLine(2 * var) doesn't contain any hint that you would want to convert var to type Term. The compiler sees the int and multiplication operator and a variable of type 'Variable'.
Edit: To clarify, in order for your example to work the compiler would have to go through all the types in scope and see if one just happens to have an implicit conversion from type 'A'.
And if there also happens do be a class C like:
class C
{
public static implicit operator C(A a) { return new A(); }
public static B operator *(int i, C c) { return new C(); }
}
there's no telling what would happen.
Which is why the compiler doesn't do that :)
If you want to make your example to work, you must move the implicit operator that converts A to B to the class A, like this:
namespace Temp
{
class A
{
public static implicit operator B(A a) { return new B(); }
}
class B
{
public static B operator *(int c, B b) { return new B(); }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(2 * ((B)new A()));
}
}
}
But you cant use this 2 * new A() because(Jon Skeet answer):
Basically, operator overload resolution doesn't include implicit user-defined conversions in order to find the operators that could be applicable.
https://stackoverflow.com/a/34161798/815590

How do implicit conversions work with operator overloading?

If I were to, say, create a thin wrapper around the float type for whatever reason, like so:
public class WrappedFloat
{
private float value;
public WrappedFloat(float value)
{
this.value = value;
}
public static implicit operator float(WrappedFloat wrapped)
{
return wrapped.value;
}
public static implicit operator WrappedFloat(float value)
{
return new WrappedFloat(value);
}
}
This code is apparently perfectly valid:
new WrappedFloat(4.0F) + new WrappedFloat(3.0F)
Since WrappedFloat doesn't define any arithmetic operators, there must be something about it's implicit conversion to float allowing this. But operator overloading is just syntactic sugar for methods, right? It's not like I can call methods (i.e. CompareTo) on WrappedFloat just because float has them. So what's so special about operators here? What are the rules for allowing this?
Operators are, for the purposes of this discussion, just like static methods. Imagine you had an actual static method for each operator overload:
public static int Plus(int a, int b) { return a + b; }
public static float Plus(float a, float b) { return a + b; }
public static long Plus(long a, long b) { return a + b; }
public static string Plus(string a, string b) { return a + b; }
public static double Plus(double a, double b) { return a + b; }
//...
Now imagine you have:
Plus(new WrappedFloat(4.0F), new WrappedFloat(3.0F));
What would you expect to happen here? Overload resolution would run, it would see that there is an overload for which both of the parameters have an implicit conversion to, and that overload (accepting float values) becomes the unique best match.
The exact same thing happens with operators. It pulls together all of the overloads, sees which ones have argument lists for which the provided arguments can be implicitly converted to, and then chooses the best match among them if there are multiple.
But operator overloading is just syntactic sugar for methods, right?
It is not syntactic sugar for "methods", it is syntactic sugar for one kind of method implemented in a specific way. The typical method call on an object with . syntax is not applicable here. MS chose a design for programmer convenience. When I call
wrappedFloat.Method()
I dont expect it to look for every possibility on wrappedFloat, but when I do
wrappedFloat1 + wrappedFloat2
I expect it to. I agree with MS here. One difference here, is that in first case it is a call on the instance, the second one is a static method call where the formal parameter is passed as an argument.

How to make binary multiplication operator * available in a generic class?

abstract class Shape<T>
{
public abstract T Area();
}
class Square<T> : Shape<T>
{
T side;
public Square(T side)
{
this.side = side;
}
public override T Area()
{
return this.side * this.side;
}
}
Error 1 Operator '*' cannot be applied to operands of type 'T' and of type 'T'.
Compiler throws an error because there is no * for this.side*this.side. How to make the binary multiplication operator * available in a generic class?
You can't use the * operator itself, but you can work around it by generating an expression that uses this operator:
static class Operators
{
public static T Multiply<T>(T x, T y)
{
return OperatorCache<T>.Multiply(x, y);
}
static class OperatorCache<T>
{
static OperatorCache()
{
Multiply = MakeBinaryOperator(ExpressionType.Multiply);
}
static Func<T, T, T> MakeBinaryOperator(ExpressionType type)
{
var x = Expression.Parameter(typeof(T), "x");
var y = Expression.Parameter(typeof(T), "y");
var body = Expression.MakeBinary(type, x, y);
var expr = Expression.Lambda<Func<T, T, T>>(body, x, y);
return expr.Compile();
}
public readonly static Func<T, T, T> Multiply;
}
}
You can then use it like this:
public override T Area()
{
return Operators.Multiply(this.side, this.side);
}
Of course you can add other operators to the Operators class; just keep in mind that if the operator you're using is not defined for T, it will fail at runtime.
You can't do that. The multiplication operator is not defined for all types. But your generic has no constraints so the compiler has to assume that the consumers of your class can use any type, like string for example.
Unfortunately, generic constraints in .NET can't be used to express this kind of requirement, i.e. there is no way to constrain T to only types that define the multiplication operator.
Bottom line is: You can't use generics in your scenario. You need to go the same way as the .NET framework with its Size (for double) and SizeF (for float) types.
Note about the context of my answer:
If you want to provide your class for arbitrary classes you have control over, Alexey has the correct answer.
However, his answer does not apply if you want to use your Square class with types like double, float or int, because you can't add an interface to them.
You can substitute it with a method .Multiply(T a, T b)
And use it with interface
public interface IMultiplyable<T>
{
T Multiply(T a, T b);
}

Casting Between Data Types in C#

I have (for example) an object of type A that I want to be able to cast to type B (similar to how you can cast an int to a float)
Data types A and B are my own.
Is it possible to define the rules by which this casting occurs?
Example
int a = 1;
float b = (float)a;
int c = (int)b;
Yes, this is possible using C# operator overloading. There are two versions explicit and implicit.
Here is a full example:
class Program
{
static void Main(string[] args)
{
A a1 = new A(1);
B b1 = a1;
B b2 = new B(1.1);
A a2 = (A)b2;
}
}
class A
{
public int Foo;
public A(int foo)
{
this.Foo = foo;
}
public static implicit operator B(A a)
{
return new B(a.Foo);
}
}
class B
{
public double Bar;
public B(double bar)
{
this.Bar = bar;
}
public static explicit operator A(B b)
{
return new A((int)b.Bar);
}
}
Type A can be cast implicitly to type B but type B must be cast explicitly to type A.
Assuming you want that to be an explcit operation you'll need to write an explicit cast operator like so:
public static explicit operator MyTypeOne(MyTypeTwo i)
{
// code to convert from MyTypeTwo to MyTypeOne
}
You can then use it like so:
MyTypeOne a = new MyTypeOne();
MyTypeTwo b = (MyTypeTwo)a;
I'd question whether you want to actually cast one type to another, or whether you actually want to convert instead. I'd say you should avoid writing cast operators for conversions, if you are just aiming to take advantage of a nice syntax :)
Also, in general it is advised not to use implicit casts, as they allow for unintended type converstions. From MSDN documentation on implicit:
However, because implicit conversions
can occur without the programmer's
specifying them, care must be taken to
prevent unpleasant surprises. In
general, implicit conversion operators
should never throw exceptions and
never lose information so that they
can be used safely without the
programmer's awareness.
You cant overload the cast operator in c# but you can use explicit and implicit conversion operators instead:
"Using Conversion Operators (C# Programming Guide)"

Categories

Resources