string.Intern does have a side-effect but sonarqube says it doesn't

  • Version 7.0 (build 36138)

The following code is marked as a bug from sonarqube:

foreach (string str in stringsToIntern) string.Intern(str);

It says “Use the return value of method ‘Intern’, which has no side effect.”. Actually, string.Intern does have a side-effect. It actually interns the string if it’s not interned already. Hence, this should not be marked as a bug.

1 Like

Thanks @dktue I’ve opened issue 2382 to track this FP

Hello @dktue,

Regarding this issue, indeed the string.Intern method does have the side-effect of adding the string value to the intern pool if it is not already there.

However, I fail to see what is the point of not using the returned value of the method: the string given as parameter will still reference the string value that was created on the heap (assuming it was not a value already in the intern pool); only the string reference returned by the method is ensured to point to the value inside the intern pool.

Do you have use case where it makes sense to not use the returned value of the Intern method?

Otherwise, we will simply update the message for this method so that it does not mention side-effect, as I agree that the wording is wrong in that case.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.