Partager via


operator (C# Reference) 

The operator keyword is used to declare an operator in a class or struct declaration. An operator declaration can take one of the following four forms:

public static result-type operator unary-operator ( op-type operand )
public static result-type operator binary-operator (
      op-type operand,
      op-type2 operand2
)
public static implicit operator conv-type-out ( conv-type-in operand )
public static explicit operator conv-type-out ( conv-type-in operand )

Parameters

  • result-type
    The type of the operator's result.
  • op-type
    The type of the first (or only) parameter.
  • operand
    The name of the first (or only) parameter.
  • binary-operator
    One of: +   -   *   /   %   &   |   ^   <<   >>   ==   !=   >   <   >=   <=
  • op-type2
    The type of the second parameter.
  • operand2
    The name of the second parameter.
  • conv-type-out
    The target type of a type conversion operator.
  • conv-type-in
    The type of the input to a type conversion operator.

Remarks

The first two forms declare user-defined operators that overload built-in operators. Note that not all the built-in operators can be overloaded (see Overloadable Operators). At least one of op-type and op-type2 must be the enclosing type (that is, the type of which the operator is a member). This prevents redefining the integer addition operator, for instance.

The last two forms declare conversion operators. Exactly one of conv-type-in and conv-type-out must be the enclosing type (that is, a conversion operator can only convert from its enclosing type to some other type, or from some other type to its enclosing type).

Operators can only take value parameters, not ref or out parameters.

Any operator declaration can be preceded by an optional Attributes (C# Programming Guide) list.

Example

The following is an extremely simplified class for rational numbers. It overloads the + and * operators to perform fractional addition and multiplication, and also provides an operator that converts fractions to doubles.

// cs_keyword_operator.cs
using System;
class Fraction
{
    int num, den;
    public Fraction(int num, int den)
    {
        this.num = num;
        this.den = den;
    }

    // overload operator +
    public static Fraction operator +(Fraction a, Fraction b)
    {
        return new Fraction(a.num * b.den + b.num * a.den,
           a.den * b.den);
    }

    // overload operator *
    public static Fraction operator *(Fraction a, Fraction b)
    {
        return new Fraction(a.num * b.num, a.den * b.den);
    }

    // define operator double
    public static implicit operator double(Fraction f)
    {
        return (double)f.num / f.den;
    }
}

class Test
{
    static void Main()
    {
        Fraction a = new Fraction(1, 2);
        Fraction b = new Fraction(3, 7);
        Fraction c = new Fraction(2, 3);
        Console.WriteLine((double)(a * b + c));
    }
}

Output

0.880952380952381

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 7.2.2 Operator overloading

  • 7.2.3 Unary operator overload resolution

  • 7.2.4 Binary operator overload resolution

See Also

Tasks

How to: Implement User-Defined Conversions Between Structs (C# Programming Guide)

Reference

C# Keywords
implicit (C# Reference)
explicit (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference