Search code examples
c#.netoperator-overloadingclrmscorlib

Where can I see operator overloads of primitive types by using ILSpy?


Just out of curiosity I was trying to find the relevant operator overloads for unary operators like +,-,* etc. for Int32 in MsCorLib.dll

I use ILSpy.

When I checked the System.Int32 struct definition, I could not see any operator overloading related.

Q: Do I look at the wrong place -then where should I look to see operator overloads for primitive types- or I should not even expect to see those methods?


Solution

  • The operators are part of C# and defined in the C# Language Specification.

    Example:

    7.7.2 Unary minus operator

    [...] The predefined negation operators are:

    • Integer negation:

      int operator –(int x);
      long operator –(long x);
      

      The result is computed by subtracting x from zero. [...]

    • Floating-point negation:

      float operator –(float x);
      double operator –(double x);
      

      The result is the value of x with its sign inverted. If x is NaN, the result is also NaN.

    • Decimal negation:

      decimal operator –(decimal x);
      

      The result is computed by subtracting x from zero. Decimal negation is equivalent to using the unary minus operator of type System.Decimal.