1

Unable to overloaded unary operator = in c#. it is giving error.

public static Result operator =(Int32 iv)
{
    return new Result(){iv=iv};
}
Thulasiram
  • 8,258
  • 8
  • 45
  • 52
  • Perhaps a use-case would help. As it stands there is only one correct answer: you can't. If we knew how/why you were planning on using this, we might be able to offer better methods. – Jonathon Reinhart Jan 20 '14 at 22:59

2 Answers2

2

You cannot overload the = operator in C#.

From 7.2.2 Operator overloading:

In particular, it is not possible to overload member access, method invocation, or the =, &&, ||, ?:, checked, unchecked, new, typeof, as, and is operators.

Jonathon Reinhart
  • 124,861
  • 31
  • 240
  • 314
2

You cannot overload the assignment operator in C#.

However, there is a good solution here.

Community
  • 1
  • 1
nvoigt
  • 68,786
  • 25
  • 88
  • 134