Unforgiven said:
I don't really understand what you mean by "correct" here. Care to
elaborate?
You can check the pdf or the ppt in the links that I posted.
Essentially it means that there is no need for Dispose() definitions in
C++/CLI but the compiler generates the Dispose from the Destructor that
you may define (including chaining calls to Dispose).
Copying from the pdf presentation:
1) Side By Side: Using a StreamReader
C++:
String^ ReadFirstLineFromFile( String^ path ) {
StreamReader r(path);
return r.ReadLine();
}
C#:
String ReadFirstLineFromFile( String path ) {
using ( StreamReader r = new StreamReader(path) ) {
return r.ReadLine();
}
}
Java:
String ReadFirstLineFromFile( String path ) {
StreamReader r = null;
String s = null;
try {
r = new StreamReader(path);
s = r.ReadLine();
} finally {
if ( r != null ) r.Dispose();
}
return s;
}
2) Side By Side: Using “lock”
C++:
{
lock l( obj );
… do something with shared state …
}
C#:
lock( obj ) {
… do something with shared state …
}
Java:
Monitor.Enter( obj );
try {
… do something with shared state …
} finally {
Monitor.Exit( obj );
}
3) Side By Side: Nontrivial “lock”
C++:
{
lock l( obj, 10 );
… do something with shared state …
}
C#:
if( !Monitor.TryEnter( obj, TimeSpan.FromSeconds( 10 ) ) ) {
throw new Something();
}
try {
… do something with shared state …
} finally {
Monitor.Exit( obj );
}
Java:
if( !Monitor.TryEnter( obj, TimeSpan.FromSeconds( 10 ) ) ) {
throw new Something();
}
try {
… do something with shared state …
} finally {
Monitor.Exit( obj );
}
4) Side By Side: Nontrivial “lock”
C++:
{
lock l( obj, 10 );
… do something with shared state …
}
C#:
using( new Lock( obj, 10 ) ) {
… do something with shared state …
}
public class Lock
: IDisposable {
private object target;
public Lock(
object o, double tm
) {
target = o;
if( !Monitor.TryEnter( o, tm ) )
throw new Something();
}
void IDisposable.Dispose() {
Monitor.Exit( target );
#if DEBUG
GC.SuppressFinalize( this );
#endif
}
#if DEBUG
~Lock() {
Diagnostics.Debug.Fail(
"Undisposed lock“
);
}
#endif
}
Also, saying C#/CLI is nonsense, there is no C# without CLI so just
saying C# will do.
The official standard is C#/CLI.