Is it possible to overload the default function operator (the () operator) in C#? If so - how? If not, is there a workaround to create a similar affect?
Thanks,
Asaf
EDIT:
I'm trying to give a class a default operator, something along the lines of:
class A {
A(int myvalue) {/*save value*/}
public static int operator() (A a) {return a.val;}
....
}
...
A a = new A(5);
Console.Write(A());
EDIT 2:
I've read the spec and I understand there's no straight forward way to do this. I was hoping there's a workaround.
EDIT 3: The motivation is to make a class, or an instance behave like a function, to make a convenient logging interface. By the way, this is doable and reasonable in C++.
There is not. Section 7.2.2 of the C# specification defines the overloadable operators as:
UNARY: + - ! ~ ++ -- true false
BINARY: + - * / % & | ^ << >> == != > < >= <=
Your readability would go to all hell anyway. Maybe there's another way to achieve what you're trying to do?