Implicit operators release data type conversions in a clear way, being very useful with complex types
Explicit operators require to code the conversion, being more specific of conventional conversions
Overload operators allow to write code for the operation. Overload is for Unary and Binary operators
In this post we implement an example for each case with the next code:
Explicit operators require to code the conversion, being more specific of conventional conversions
Overload operators allow to write code for the operation. Overload is for Unary and Binary operators
In this post we implement an example for each case with the next code:
public class Qubit
{
public double Spin { get; set; }
public Qubit(double spin)
{
Spin = spin;
}
public static implicit operator EntangledQubit(Qubit qubit)
{
var quantizedAngularMomentum = 3.14159;
qubit.Spin *= 2;
return new EntangledQubit(qubit.Spin * quantizedAngularMomentum * 2);
}
}
public class EntangledQubit
{
public double Spin { get; set; }
public EntangledQubit(double spin)
{
Spin = spin;
}
public static explicit operator Qubit(EntangledQubit entangledQubit)
{
var quantizedAngularMomentum = 3.14159;
entangledQubit.Spin /= 2;
return new Qubit(entangledQubit.Spin / quantizedAngularMomentum / 2);
}
public static EntangledQubit operator ++(EntangledQubit entangledQubit)
{
entangledQubit.Spin *= 2;
return entangledQubit;
}
}
Qubit qubit = new Qubit(256);
// implicit
EntangledQubit entangledQubit = qubit;
// explicit
Qubit qubit2 = (Qubit)entangledQubit;
// overload
entangledQubit++;