Writing a literal as 0; as an instruction is not allowed because
As an operator
only assignment, call, increment, decrement and new object expressions can be used.
Obviously, this does not explain why the C # compiler command did not allow the use of such literals, but I would suggest that this is either because
- creating a parser or compiler was easier
- he removed the source of potential problems or errors
The reason new ClassName(); allowed as an operator, without storing it in a variable, is that designers can introduce side effects, which may be what the developer wanted to do. Failure to do so would mean that the developer had to introduce a meaningless variable without any advantages.
The question remains open why new int(); allowed, although using literal 0; is not. Once again, I do not know the answer, but possible reasons may be
- since
new ClassName(); allowed, then new int(); should be too - creating an exception for a rule in which โnew object expressions can be used as an expressionโ would be of little use, but would mean more work and more tests and more money spent.
source share