1

A R# inspection of my solution told me "'Local variable 'fs' is never used'" about this line:

var fs = new FormatString();

Okay, then; just get rid of the whole shebang, right?

Instead, R#'s action was to remove just the var declaration and assignment, leaving:

new FormatString();

To my chaprise (chagrined surprise), it compiles!

But does it make any kind of sense?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

3 Answers3

5

Theoretically, yes. An object is constructed; the constructor executes. It might well do something "interesting" in the process.

Jon
  • 428,835
  • 81
  • 738
  • 806
4

If there are side effects in the constructor, then it's important for it to run. If it is side effect free, then you can remove the whole thing.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    Is the loading assembly to the appdomain the side effect? I don't think so. But it can have meaning. – Hamlet Hakobyan Mar 19 '14 at 20:59
  • 3
    @HamletHakobyan It depends on how literally you want to get. Technically one can consider anything that causes time to pass to be a side effect, if you consider the current time to be a part of your program's state, but, generally speaking, that is not taken to be the case. Actions that cause time to pass without altering any other environment state tend to be considered side effect free. I wouldn't consider loading an assembly a side effect, at least in most contexts, with the possible exception of a task whose goal is to pre-load a bunch of assemblies. – Servy Mar 19 '14 at 21:00
3
public FormatString()
{
    LaunchTheNukes();
}

in this case, yes it's doing 'something'.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112