How are property assignment expressions handled in C#?
- by Serious
In C# you can use a property as both an lrvalue and rvalue at the same time like this :
int n = N = 1;
Here is a complete C# sample :
class Test
{
static int n;
static int N
{
get { System.Console.WriteLine("get"); return n; }
set { System.Console.WriteLine("set"); n = value; }
}
static void Main()
{
int n = N = 1;
System.Console.WriteLine("{0}/{1}", n, N);
}
}
You can't do that in C++/CLI as the resulting type of the assignment expression "N = 1" is void.
EDIT: here is a C++/CLI sample that shows this :
ref class A
{
public: static property int N;
};
int main()
{
int n = A::N = 1;
System::Console::WriteLine("{0}/{1}", n, A::N);
}
So what's the magic behind C# syntax allowing a void-expression to be used as a rvalue ?
Is this special treatment only available for properties or do you know other C# tricks like this ?