1

The related post for System.NullReferenceException doesn't help, please avoid linking that or marking as duplicate!

Code purpose: Check if the docType (a string variable) is present on a list of strings replaceEntitiesConfig.DocTypes.

Code that works:

private bool MustReplaceEntities(DocumentRequestModel docRequest, DocumentRequestOptions requestedInfo) { var replaceEntitiesConfig = this.ConfigurationManager.GetModel<ReplaceEntitiesConfigModel>(ConfigurationModels.ContextualMenus, this.ApplicationContext.Application.ProductId); if (requestedInfo.Text && !replaceEntitiesConfig.DocTypes.IsEmpty()) { var docType = this.GetDocument(docRequest, new DocumentRequestOptions { DocType = true }, DecoratorTypes.None).DocType; foreach (var configuredDocType in replaceEntitiesConfig.DocTypes) { if (configuredDocType.Equals(docType, StringComparison.InvariantCultureIgnoreCase)) { return true; } } } return false; } 

Code that doesn't work:

private bool MustReplaceEntities(DocumentRequestModel docRequest, DocumentRequestOptions requestedInfo) { var replaceEntitiesConfig = this.ConfigurationManager.GetModel<ReplaceEntitiesConfigModel>(ConfigurationModels.ContextualMenus, this.ApplicationContext.Application.ProductId); if (requestedInfo.Text && !replaceEntitiesConfig.DocTypes.IsEmpty()) { var docType = this.GetDocument(docRequest, new DocumentRequestOptions { DocType = true }, DecoratorTypes.None).DocType; return replaceEntitiesConfig.DocTypes.Any(x => x.Equals(docType, StringComparison.InvariantCultureIgnoreCase)); } return false; } 

With LINQ statement, exception is thrown, and the exception is thrown even before docType is initialized. Without it (using foreach instead), it works perfectly. Both codes should work the same way (even ReSharper suggests me to change the foreach for the LINQ expression), but with LINQ it's failing.

For the case that it doesn't work, it doesn't go any further than this line var docType = this.GetDocument(docRequest, new DocumentRequestOptions { DocType = true }, DecoratorTypes.None).DocType; though it worked before (the only change is that after this line the program logic is done with a LINQ statement instead of a foreach)

Values:

  • docType = "LE" (I see it from the way exception is not thrown, or in the case it's thrown, debugging the GetDocument method).
  • replaceEntitiesConfig.DocTypes = new List<string>();
1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented May 11, 2015 at 14:08

1 Answer 1

1

You're probably building Release configuration, where some optimizations are in effect. In your case what happens is that compiler deduces that string docType is not used, so it removes it from code, and then compiler figures out that DocumentResponseModel responseForDocType is not used as well, which is also removed from code. Non-existent code does not cause exceptions.

But once one of the lines that actually uses responseForDocType is introduced, we also must have the line that creates the thing, and then we have the exception.

TL;DR - build in Debug configuration, and see the difference.

Sign up to request clarification or add additional context in comments.

7 Comments

The compiler is not allowed to perform optimizations that change observable program behavior.
Building in Debug. Happens in Debug :/. I already though of that
@Juan maybe the option "Optimize code" is turned on in debug build. Maybe you build debug, but still run release.
@Dialecticus Just checked. Debug has "Optimize code" unchecked. "Release" has it checked
Also, user usr has a point, there is not enough information in the question, so the question is at fault. I sense we're going to "overchat" the issue, so I better stop it now, before it takes more time from me.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.