0

I've seen some snippets of code do the following:

CalculateSum mySum = new CalculateSum();
{
  mySum.add(50);
};

while other developers do it this way (without the enclosing braces:

CalculateSum mySum = new CalculateSum();

mySum.add(50);

I understand that the first method ensures that the object is disposed of as soon as it falls out of scope but I thought that .NET garbage collector works well enough that you do not need the above.

Am I wrong? Is it still best practice?

  • 5
    Are you sure the first example is exactly what you saw? Wasn't there the keyword `using` in it? Right now, there's absolutely no difference between the first and second version. – Andy Oct 23 '15 at 14:01
  • 4
    [Is the usage of internal scope blocks within a function bad style?](http://programmers.stackexchange.com/questions/200709/is-the-usage-of-internal-scope-blocks-within-a-function-bad-style) – gnat Oct 23 '15 at 14:09
  • 2
    Maybe You're confusing it with object initialization syntax: https://msdn.microsoft.com/en-us/library/bb384062.aspx – Grzegorz Sławecki Oct 23 '15 at 14:17

0 Answers0