Possible Duplicate:
Why do assignment statements return a value?
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.
EDIT1: 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);
}
EDIT2: Moreover this is really confusing : what I was expecting was that the getter would be call to assign to n which is not the case at all; n directly receives the right-most value of the expression, i.e. 1 like shown by the generated CIL :
IL_0001: ldc.i4.1
IL_0002: dup
IL_0003: call void Test::set_N(int32)
IL_0008: nop
IL_0009: stloc.0
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 ?
EDIT : As pointed out C# assignment rules say that rvalue should ALWAYS be used as the return value of an assignment.