Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Is this a CLR restriction or a language design decision? I tried to do it in C++/CLI, of course where it works because the need to support native c++:

public ref class Test
        public:
        static Test^ operator &( Test^ msg, int& i )
            i = i + 1;
            return nullptr;

and then looked at the compiler omitted output:

public: static Test __gc* op_BitwiseAnd(Test __gc* msg, Int32 __gc** modopt(IsImplicitlyDereferenced __gc*) i)
    i[0] += 1;
    return 0;

I went further and tried to call this operator from C# project - and of course I needed to go [unsafe] to do it( I needed pointer ):

Test t = new Test();
int i = 0;
unsafe
    t = t & &i;

Obviously not so hard to implement for the CLR? I really miss pass by reference in operators overloading and would like to at least in light myself in why is this missing?

Why can't C# hide the ugliness behind the unsafe and pointers when we need to deal with reference variables in our operator overloads? Even if I chose to go with this ugly workaround it wouldn't work in Silverlight, where unsafe operations is not allowed...

In C#, your variables are never altered by callees without you explicitly passing them as references (e.g. int.TryParse(s, out i) where you explicitly specify the out keyword). This feature would make things complicated by allowing the overloaded operator alter the contents of the operands without your explicit permission.

For instance,

public static MyStruct operator + (ref MyStruct left, ref MyStruct right) {
    left = new MyStruct(); // !!!!!!!!
    return something(left, right);

When you reference such an operator in C#:

MyStruct x = new MyStruct();
MyStruct y = new MyStruct();
MyStruct z = x + y; // in C#, you never expect `x` to be changed.
                Ivan: C# is designed not to clone C++ complexities. "What if I really need to alter a operand that is not a reference type - how do I do it?" First, if you really need it, you should create a method, not an operator for that purpose. You are abusing operator overloading. Second, what if the callee isn't conscious of your decision and suddenly sees his variable modified in an expression? This will cause headaches in debugging.
– Mehrdad Afshari
                Oct 31, 2009 at 16:19
                "What if I really need to alter a operand that is not a reference type - how do I do it?" Use a method instead. I don't think there is ever a situation where you really need an operator, specifically, that works this way.
– Joren
                Oct 31, 2009 at 16:19
                Allowing types to override things like += with a method whose first parameter was a ref type would yielded semantics entirely in line with expectations, but would have allowed certain types to implement such operators in a fashion which was more efficient or offered better thread safety than would be possible as the += operator is normally implemented.
– supercat
                Jun 2, 2014 at 18:44
                I'd have to agree that this could be inlined in a predictable way and would definitely have benefits. Prime example: operator overloading on user-defined matrix types for use in a graphics application. A 4x4 matrix is a bombshell to pass around by value at each call, which is why we often resort to creating function calls that make use of pass-by-ref functionality even for methods such as equality comparisons where we have no intentions of allowing either operand to be changed. Allowing the same semantics for operator overloading would be wonderful for this.
– Mike Johnson
                Aug 25, 2014 at 0:20

I think it is because an operator (in the more mathematical view that C# seems to take) is logically something that combines its arguments into a new value, never supposed to be mutating anything. C++ seems to consider operators more as a version of general operations with more convenient syntax than functions, than as a way to represent maths in particular. I think that in C# you'll almost never see things like defining operators for stream operations and such.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.