Why are some useless statements partially resolved?

I really don't understand why the C # compiler allows some useless statements, but disallows some other useless statements.

static void Main() { string('a', 1); // useless but allowed //"a";// useless and disallowed new int(); // useless but allowed //0;// useless and disallowed new char(); // useless but allowed //'\0';// useless and disallowed new bool(); // useless but allowed //false;// useless and disallowed //new int[] { 1, 2 };// useless and disallowed //new [] { 1, 2 };// useless and disallowed //new int[2];// useless and disallowed //new int[2].Length;// useless and disallowed int[] var = new int[2]; // useful //var.Length;// useless and disallowed string s = string.Empty; // useful //string.Empty;// useless and disallowed } 
+6
source share
4 answers

These are not "useless" statements. You call the constructor, and you don't know what the constructor can do - it can have important side effects. This can be a material recording, connection initialization, etc. Although this is bad practice, the compiler cannot force you to delete them.

As for the other statements:

  • 0 , false : integer and boolean literals have no side effects.
  • arr.Length , properties should not have any side effects (although they may have).

Therefore, it is safe to force the developer to remove these claims.

+8
source

These statements are not worthless; you simply do not store their meanings. But you can write, for example,

 Console.WriteLine(new int()); // output: 0 

A constructor call is a valid statement that creates a value, the problem is that you are not assigning this value to any variable so that it is not available later.

+2
source

Method and constructor calls are allowed, as they can have side effects.

There are no other things, because they do not have side effects (or should not, as in the case of properties).

+2
source

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.
+2
source

All Articles