Overloading Operators

  • 18 years ago

    I've seen that it's possible to overload operators in C#,
    but is it possible for me to get my << for output back?


    public String operator <<(Console a, String b) {
    a.WriteLine(b);
    }


    Gives the error:


    error CS0563: One of the parameters of a binary operator must be
    the containing type.


    What exactly does this mean?


    Thanks,
    - Mike


  • 18 years ago

    That means that if you've declared this operator within a class called "Fred", then you need to do


    public String operator <<(Console a, Fred b) {
    a.WriteLine(b);
    }


    or something wierd like that (according to the documentation on that error):


    // CS0563.cs
    public class iii
    {
      public static int operator <<(int aa, int bb)   // CS0563
      // try the following line instead
      // public static int operator <<(int aa, iii bb)
      {
         return 0;
      }


      public static void Main()
      {
      }
    }

  • 18 years ago

    That makes sense.


    Thanks,
    - Mike

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Brevity is the soul of wit” - Shakespeare