5

I was looking at this and it its starting comments it says it exists because

throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key"));

generates more IL code than

ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key, ExceptionResource.ArgumentNull_Key);

which I can't find in the code, but like its similar methods I assume just calls throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); or something.

Does the ThrowHelper class really help lessen the IL code generated or is it the same (or more) considering the IL code generated from ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key, ExceptionResource.ArgumentNull_Key);?

EDIT: The IL shown at the start of the file, in the comments, is only from the call to ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key, ExceptionResource.ArgumentNull_Key); not the actual code inside that method. Or am I wrong?

user14092802
  • 105
  • 10
  • 1
    It literally shows you the IL at the top of that file... – Ian Kemp Aug 12 '20 at 11:49
  • @IanKemp, it shows the IL generated from the `ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key, ExceptionResource.ArgumentNull_Key);` it does not show the IL generated from the `ThrowArgumentNullException`'s inner workings. – user14092802 Aug 12 '20 at 12:02
  • 3
    The IL from the "inner workings" is irrelevant since it only occurs once. Of course the *total* size of IL is larger if you only call `ThrowArgumentNullException` once, but the trick is that it's called many times. – Jeroen Mostert Aug 12 '20 at 12:05
  • 3
    And in particular, reducing the IL size in many methods can be important for inlining. – Jon Skeet Aug 12 '20 at 12:05
  • 1
    Nothing to do with the IL, it is used to let the jitter generate better machine code. Methods that contain `throw` cannot be inlined. Inlining is one of the basic [machine code optimizations](https://stackoverflow.com/a/4045073/17034), the method entirely disappears and the code is generated inside the calling method. By using ThrowHelper that problem is solved, exceptions don't need to be fast. It is a micro-optimization, which are evil but the kind of thing you have to do when you create a framework and cannot guess where your methods are being used. – Hans Passant Aug 12 '20 at 12:07

0 Answers0