How to create operators in C #?


How to create a C # operator? For example,

There are operators like:

* multiplicacao
/ divisao
% percentual

The question is:

How can I create my own operator? For example:

100 ~ 2 = 200.8

Where ~ would be my operator that would do something like:

A ~ B = ((A * 2) + (B * 2)) / (5) 
100 ~ 2 = ((100 * 2) + (2 * 2)) / 5 = 200.8
asked by Roger Oliveira 29.09.2015 в 05:50

2 answers


Creating an operator is not possible in the language. So the ~ operator will only exist if the language designers one day wish and I doubt that this will occur.

Some existing operators, not all, may have their behavior changed in a particular type - not overwritten. The term used is overhead .

In any case, the example used is not possible. The two expressions make no sense. Because in language = is the assignment operator ( it can not be overloaded ) on the left side of it ( lvalue ) it is not possible to use other operators as used.

Although this was a typo and the operator there was == (which can be overloaded), the second example would not make sense in the language. And, of course, the ~ operator does not exist.

If you want to overload the operators you can and use in the correct place, then I say do not.

Are you sure you know what you're doing? Do you know all the implications? Are you going to do something useful with him? Is it intuitive for who will use it? That is, the programmer who will use your operator will understand what is going on there?

Very few types need to have their own operators and they are the ones that have equivalence in mathematics. Preferably in obvious things. It is not to abuse the overhead of operators. It is not to create for anything, it is not to make creative use. It has language that has chosen not to have it precisely to avoid abuse (which I disagree with, because anything can be abused).

If you're really going to do this, a good start is to read through the tutorial "official" very carefully, read the documentation at specification and search a lot about it.

Understand that operators are static . Understand that some operators may suggest behavior that requires specific code so that everything goes well (only an example of this).

Perhaps the operator that makes the most sense in some types is implicit or explicit cast . And this hardly anyone thinks. People are not that creative. Even this can be abused. But it is more common for you to require an operator to convert from one type to another than to pretend that a + symbol does something different, after all this symbol should only be used for addition, preferably numerical. There is controversy as to whether it should be used for text, as it is used in language. Even so, there are those who prefer to do a type converter method than to use the operator in cases not so clear.

Another operator where overloading is common is that of equality, where it has been said that you need to make sure you understand how it works.

A real example of use in a type that makes sense (extremely simplified form):

using static System.Console;
class Complex {
    private int real;
    private int imaginary;
    public Complex(int i, int j) {
        real = i;
        imaginary = j;
    public override bool Equals(object o) {
        return ((Complex)o).real == this.real && ((Complex)o).imaginary == this.imaginary;
    public override string ToString() {
        return string.Format("{0} + {1}i", real, imaginary);
    public override int GetHashCode() {
        return this.ToString().GetHashCode();
    public static bool operator == (Complex x, Complex y) {
        return x.Equals(y);
    public static bool operator != (Complex x, Complex y)   {
        return !x.Equals(y);
    public static Complex operator +(Complex x, Complex y) {
        return new Complex(x.real + y.real, x.imaginary + y.imaginary);
public class Program {
    public static void Main() {
        var x = new Complex(10,20);
        var y = new Complex(10,20);
        var z = y;
        if (x == y) {
            WriteLine("z igual y");
        } else {
            WriteLine("x diferente y");
        if (y != z) { 
            WriteLine("y diferente z"); 
        } else {
            WriteLine("y igual z");

See working on dotNetFiddle .

I will reinforce: almost all the ideas that people have to do operator overloading should not be made. The best places where it fit was already done in the language.

29.09.2015 / 14:27

I do not know if it would be possible to create a unique symbol for you, but you can override a normal operator using a class.

public class TesteOperador
    public float valor

    public TesteOperador (float valor)
        this.valor = valor;

    //ou retornando um tipo especifico que você queira... int, float
    public static TesteOperador operator + (TesteOperador A, TesteOperador B)
        return new TesteOperador((A.valor * 2) + (B.valor * 2) / (5));


29.09.2015 в 08:47