Previous month:
March 2005
Next month:
May 2005

April 2005

C++/CLI, IDisposable and Finalize

With Beta 2, C++/CLI changed the code that is generated with the destructor (~Class) and the explicit finalize (!Class). It's a great improvement! Now the Dispose(true) pattern for embedded objects is implemented with this code that just contains a destructor and a explicit finalize:

ref class Resource
{
public:
  ~Resource() // IDisposable
  {
    // release resource
  }

protected:
  !Resource() // Finalize
  {
    // release resource
  }

};

The result of this C++/CLI code not only implements the IDisposable interface and overrides the Finalize method (as it happened with earlier releases), but also implements Dispose(true) as can be verified with the IL code:

ref class Resource : IDisposable
{
public:
  void Dispose()
  {
    Dispose(true);
    GC.SupressFinalize(true);
  }
private:

  ~Resource()
  {
    // release resource
  }
internal:
  void Dispose(bool disposing)
  {
    if (disposing)
      ~Resource();
    else
    {
      try
      {
        !Resource();
      }
      finally
      {
        Object::Finalize();
      }
    }
  }
internal:
  void Finalize()
  {
    Dispose(false);
  }
private:
  !Resource()
  {

  }

};

It would be great to have such a feature with C#.

Christian


C# 2.0 and C++/CLI Event

Last week I've done an INETA sponsored talk for the .NET User Group Styria, thanks to the invitation of Klaus Aschenbrenner.

The attendees have been zealous from possibilities of generics and anonymous methods.

Some more information about generics and anonymous methods with C#:

And C++/CLI:

Christian


Friend Assemblies

The C# access modifier internal defines access is only allowed within the assembly. This is what .NET 1.0 defined. With .NET 2.0 friend assemblies are allowed to access these members, too. Friend assemblies are defined with the attribute class InternalsVisibleToAttribute:

[assembly:InternalsVisibleTo ("AssemblyB, PublicKeyToken=32ab4ba45e0a69a1")]

Christian