How are property assignment expressions handled in C#?
Posted
by
Serious
on Stack Overflow
See other posts from Stack Overflow
or by Serious
Published on 2012-11-28T10:09:21Z
Indexed on
2012/11/28
11:03 UTC
Read the original article
Hit count: 250
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 ?
© Stack Overflow or respective owner