It’s probably very lame question, but I found no references in C# specification about round brackets. Please point me to spec or msdn if answer on that question will be obvious.
What is the inner difference between (MyType)SomeObj.Property1 and (MyType)(SomeObj.Property1) in C# ?
AFAIK, in first case ( (x)SomeObj.Property1 cast ) – it will be the reference of concrete type (MyType) to Property1. In second case, such reference will execute get accessor SomeObj.get_Property1.
And it eventually could lead to subtle errors if get accessor have any side effects (and its often – do have ones)
Could anyone point me to exact documentation where such behaviour specified ?
Updated: Thank you for pointing. And I deeply apologize about such dumb question – after posting this question, I found a typo in example I fiddle with and thus realized that second case behaviour was not based on code I tried to compile, but on previously compiled completely different code. So my question was initially based on my own blindness …
They are equivalent. This is determined by the operator precedence rules in the C# language, chapter 7.2.1 in the C# Language Specification:
The
.operator is at the top in this list, the cast operator is the 2nd in the list. The.operator “wins”. You will have use parentheses if you need the cast because Property1 is a property of the MyType class: