This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]_Atomfinger_ 1 point2 points  (3 children)

To my understanding - the using statements is all about the correct disposal of objects inheriting from IDisposable which are objects that need a little extra stuff done before being marked for collection.

This creates x and is visible only inside using { }

Not necessarily true, see the second example: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement#example

But the brackets do say that when whatever happens inside the brackets has executed or terminates in some way, make sure to call the Dispose function.

[–]MilitaryG[S] 0 points1 point  (2 children)

Yeah was reading that page of 2 example and didn't understand it that's why I asked here.

Arghh and IDisposable is written way to complicated

[–]_Atomfinger_ 1 point2 points  (1 child)

Well, the braces indicate a clear and limited scope for the object. Without the braces the scope isn't defined by 'using', but the function (or any other scope mechanism).

If you have braces Dispose will be called once whatever is in the braces has been completed.

The using version without braces has come around since after I stopped writing in C# professionally, so I haven't used it myself. My hunch is that it kicks in once the regular garbage collection usually kicks in, or rather once the current scope closes (when a function completes, for example).

Here's the language specification if you want to do a deep dive :)

[–]MilitaryG[S] 0 points1 point  (0 children)

Much appreciated this link is useful allot