After serious thinking Craig Stuntz wrote :
> Brad White wrote: > > >> The reason that we have to use 'using' in C# >> is to make finalization deterministic. >> [...] >> In Delphi that is called Free. > > No. That's not right. The closest thing to Delphi's Free in C# is > IDisposable.
OK, so I didn't give a very good example. But it has to do with making sure that everything gets cleaned up deterministicly.
From the source I quoted:
> Using: Defines a scope, outside of which an object or objects > will be disposed. > > Remarks > > C#, through the .NET Framework common language runtime (CLR), > automatically releases the memory used to store objects that > are no longer required. The release of memory is non-deterministic; > memory is released whenever the CLR decides to perform garbage > collection. However, it is usually best to release limited > resources such as file handles and network connections as > quickly as possible. > > The using statement allows the programmer to specify when > objects that use resources should release them. The object > provided to the using statement must implement the IDisposable > interface. This interface provides the Dispose method, which > should release the object's resources. > >
So in effect it wraps the block with Try..Finally and calls Dispose when the objects go out of scope.
So a better parallel is an interface that goes out of scope. Sure you could clean up manually, but the point is to have it be deterministic without calling dispose explicitly.
This Delphi can do with interfaces and C# can't without using the 'using' clause.
HTH, Brad.