in

ASP.NET Weblogs

This Blog

Syndication

Christian Nagel's OneNotes

.NET Training, Consulting, Coaching - C#, Web Services, Enterprise Services, ASP.NET, Whidbey, Longhorn and More!

April 2005 - Posts

  • 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

    Posted Apr 27 2005, 09:15 PM by CNagel with 2 comment(s)
    Filed under:
  • 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

  • Birds of a Feather

    For TechEd Europe we (INETA Europe) need proposals for Birds of a Feather sessions!

    Meet other attendees with similar interests!

    Damir Tomicic has all the information about Birds of a Feather!

    Christian

    Posted Apr 05 2005, 08:55 AM by CNagel with no comments
    Filed under: ,
More Posts