Will my compiler ignore useless code?

前端 未结 9 1310
情深已故
情深已故 2021-02-06 21:53

I\'ve been through a few questions over the network about this subject but I didn\'t find any answer for my question, or it\'s for another language or it doesn\'t answer totally

9条回答
  •  旧巷少年郎
    2021-02-06 22:08

    The loop can't be removed, the code is not dead, for instance:

      // Just some function, right?
      private static Double[] SomeFunctionThatReturnDoubles() {
        return null;
      }
    
      ...
      double[] TestRunTime = SomeFunctionThatReturnDoubles();
      ...
      // You'll end up with exception since TestRunTime is null
      for (int j = 0; j < TestRunTime.Length; j++)
      {
      }
      ...
    

    Usually, compiler just can't predict all possible outcomes of SomeFunctionThatReturnDoubles and that's why it preserves the loop

提交回复
热议问题