Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>String constants are immutable and atomized, they are also reference objects. However the result of Request.QueryString["UserGroups"] is a string reference (or a reference to an object that may be implicitly converted to a string...) that may not be atomized, so you cant just compare the references which may be distinct, even if the strings are comparing equal.</p> <p>The == operator on pairs of Strings is not comparing references but string contents. This means that the Request.QueryString["UserGroups"] will be dereferenced, and this may cause null pointer dereference exceptions. That's why there's a prior test for null (because tests with "reference == null" is NOT dereferencing the reference but actually checking if it's null or not).</p> <p>There's a possibility however to avoid the null check: you may use the === operator to just compare the reference, if the result of Request.QueryString["Usergroups"] has been atomized (but it may require allocation and hashing within the static list of atoms, not a good idea if the QueryString is very large.</p> <p>So yes, the best to do is to first cache the Querystring in a local string variable, and perform the two tests:</p> <pre><code>final string queryString; // cache the string value if ((queryString = Request.QueryString["UserGroups"]) != null &amp;&amp; queryString == "True") { ... } else { ... } </code></pre> <p>But given that the bodies of your if/else statement is just to store the result of the if()'s condition, just write this:</p> <pre><code>final string queryString; // temporary register caching the non-atomized string reference report.IncludeGroupFiltering = (queryString = Request.QueryString["UserGroups"]) != null &amp;&amp; queryString == "True"; // compares the two strings contents </code></pre> <p>BUT ONLY IF the Request.QueryString[] contents are already atomized strings, or if their implicit conversion to string return atomized strings, save the string compares and use === instead:</p> <pre><code>final string queryString; // temporary register caching the atomized string reference report.IncludeGroupFiltering = (queryString = Request.QueryString["UserGroups"]) != null &amp;&amp; queryString === "True"; // compares the atomized references </code></pre> <p>I will not suggest this dangerous assumption here (it's more probable that query results from a remote source will not be atomized, for security/memory reasons, unless the returned value has already been checked. Given your code, I suspect that this is performing validation on returned values from your query, so the result is most probably not atomized: the main reason of your code is to atomize the content of the Query String into a shared boolean value, which will later compare much more easily.</p> <hr> <p>Note: I absolutely don't know what is the type of the value or reference returned by Request.QueryString["UserGroups"]. It may happen that this is an object that implements the "bool operator==(string)" method, or that even returns another type than bool. Storing the returned object in a string variable however will perform its conversion to a string if the object's type is not null (and if the object is compatible, otherwise you'll get an exception).</p> <p>You may want to avoid this conversion of the unknown object, if the object itself can compare to a string like "True", with code like this:</p> <pre><code>report.IncludeGroupFiltering = Request.QueryString["UserGroups"] != null &amp;&amp; // uses object's operator==(string) to compare its contents OR reference. Request.QueryString["UserGroups"] == "True"; </code></pre> <p>All this depends on how you declared the QueryString[] array property of your Request object, and if the array contents can be polymorphic (varying type). If you know how it's declared, then use exactly the same type for declaring the temporary final register above, to avoid double member access to QueryString from Request, and double indexing of the QueryString array.</p> <p>Here, it's impossible to know which code will be the best for you as we don't have all the declarations (C# inherits the same type complexity/ambiguity as C++, with too many implicit conversions and complex inheritance schemes).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload