C++/CLI has a great syntax for dealing with resources. The syntax of C++/CLI is simpler than C#.
To implement the interface IDisposable, with C# this syntax is needed:
// C#
public class Resource : IDisposable
{
public void Dispose()
{
Console.WriteLine("release resource");
}
}
C++/CLI is using the destructor syntax to implement the interface IDisposable:
// C++/CLI
public ref class Resource
{
public:
~Resource()
{
Console::WriteLine("release resource");
}
};
If the Resource class allocates native resources, the Finalize method of the base class should be overridden. With C# the Finalize method is overridden using a destructor syntax:
// C#
public class Resource : IDisposable
{
~Resource()
{
Dispose();
}
public void Dispose()
{
Console.WriteLine("release resource");
}
}
C++/CLI already uses the destructor syntax to implement IDisposable. To implement the Finalize method, a new syntax is available:
// C++/CLI
public ref class Resource
{
public:
~Resource()
{
this->!Resource();
}
protected:
!Resource()
{
Console::WriteLine("release resource");
}
};
If the Resource class contains embedded objects that should be disposed, this pattern is needed with C#:
// C#
public class Resource : IDisposable
{
private EmbeddedResource embedded;
public Resource()
{
Console.WriteLine("allocating resource");
embedded = new EmbeddedResource();
}
~Resource()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
Console.WriteLine("release resource");
if (disposing)
{
embedded.Dispose();
}
}
public void Dispose()
{
Dispose(true);
}
}
This pattern is already implemented with ~Resource and !Resource:
// C++/CLI
public ref class Resource
{
private:
EmbeddedResource^ embedded;
public:
Resource()
{
embedded = gcnew EmbeddedResource();
}
~Resource()
{
delete embedded;
this->!Resource();
}
protected:
!Resource()
{
Console::WriteLine("release resource");
}
};
C++/CLI also has advantages using and releasing the Resource class. With C#, the Dispose method can be invoked directly:
// C#
Resource r = new Resource();
r.Foo();
r.Dispose();
This is similar to the following C++/CLI constructs. The C++ delete gets compiled to invoking the Dispose method:
// C++/CLI
Resource^ r = gcnew Resource();
r->Foo();
delete r;
With both of these usages it was not taken into account that the Foo method may throw an exception, and thus the Dispose method is not invoked. C# has the using statement for dealing with this easily. At the end of the using scope, Dispose is invoked.
// C#
using (Resource r = new Resource())
{
r.Foo();
}
Instead of the using statement, C++/CLI allows declaring the object locally. The object is still allocated on the managed heap. However, when the object gets out of scope, the Dispose method is invoked. This is simpler than the C# using statement.
// C++/CLI
Resource r;
r.Foo();
Christian
great
Posted by: bob dawson | 03/13/2009 at 11:00 AM
came in very handy... thanks! :D
Posted by: mike | 07/14/2009 at 04:37 PM
A colleague gave me this link. Yes, very useful, especially the comparison between C# and C++.
Posted by: Abdul | 07/16/2009 at 11:13 AM
It is really nice written article from you guys. I would suggest it to everyone who wants to read and learn more. There is so many helpful information’s which I couldn’t find on other websites and other similar blogs. Thank You for that.
Posted by: web design sydney | 02/12/2010 at 05:58 AM
Wow!
Posted by: David | 12/02/2010 at 03:56 PM
Finally found what I want, I'm a beginner content inside of great help to me with you, share
Posted by: thomas sabo | 01/20/2011 at 03:15 AM
Believe in yourself you can!
Posted by: Coach Outlet | 01/21/2011 at 09:44 AM
Wow, this is the best site I’ve ever read. Thank you for sharing this.
Posted by: Hair Styles | 03/09/2011 at 06:48 AM
Thank you, creating such a wonderful site, the article is very exciting, not boring, the color is my favorite style.
Posted by: thomas sabo uk | 03/15/2011 at 10:49 AM
It is really nice written article from you guys. I would suggest it to everyone who wants to read and learn more. There is so many helpful information’s which I couldn’t find on other websites and other similar blogs. Thank You for that.
Posted by: zolpidem tablets | 03/29/2011 at 03:37 PM
The information I found here was very useful to me. Congratulations on having such an amazing resource
Posted by: Discount Oakley Sunglasses | 06/01/2011 at 10:27 AM
Skönt att vara besöka din blogg igen, har det varit månader för mig. Väl här artikeln som jag har väntat så länge. Jag behöver den här artikeln för att slutföra mitt uppdrag i kollegiet, och den har samma tema med din artikel. Tack, bra aktie.....
Posted by: Generic Viagra | 06/03/2011 at 12:58 PM
I wonder how you got so good. This is really a fascinating blog, lots of stuff that I can get into. One thing I just want to say is that your Blog is so perfect!....
Posted by: Kamagra | 06/03/2011 at 12:59 PM
Thank you
Posted by: Siffredi | 06/13/2011 at 09:45 AM
// C++/CLI
Resource r;
r.Foo();
Wow... And I was a "try...finally" animal. It's a nice feature :D
Thank you. Although you should clarify this is just syntax info in the title.
Posted by: Max | 06/14/2011 at 09:32 AM
just wanted to let you know I enjoy reading your blog and hope you will continue writing them for a long time to come.
Posted by: Cheap MBT Shoes | 06/17/2011 at 05:22 PM
Interesting posts, responses, and new site information. In the future, hopefully we will new pages and updated information.
Posted by: Ray Ban | 06/18/2011 at 11:44 AM
swarovski crystals
swarovski outlet
swarovski store
shop swarovski
buy swarovski
Swarovski bracelet
Swarovski Crystals Bracelets
Swarovski Bangles
Swarovski Earrings
Swarovski Crystal Earrings
Swarovski Clip Earrings
Swarovski Earrings Sale
Swarovski Necklace
Swarovski Crystal Necklaces
Swarovski Colliers
Swarovski Pendant Necklaces
Swarovski Ring
Swarovski Crystals Rings
Swarovski Rings On Sale
Swarovski Rings Outlet
Air Max 2010
Air Max 95
Femme Nike Air Max 95
Air Max 90
Femme Nike Air Max 90
Air Max LTD
Air Max LTD II
Femme Nike Air Max LTD
Air Max TN
Femme Nike Air Max TN
Nike Air Max Skyline
Air Max Skyline
Femme Air Max Skyline
Air Max
Nike Air Max
Air Max pas cher
Wholesale Gucci Handbags
Wholesale D&G Handbags
Wholesale Coach Wallets
Wholesale LV Wallets
Wholesale Gucci Wallets
Wholesale Chanel Wallets
Wholesale LV Sunglasses
Wholesale Gucci Sunglasses
Wholesale Chanel Sunglasses
Wholesale D&G Sunglasses
Wholesale Oakley Sunglasses
Oakley Sunglasses
Ray.Ban Sunglasses
LV Sunglasses
Gucci Sunglasses
Chanel Sunglasses
Wholesale nfl jerseys
wholesale jerseys
wholesale football jerseys
authentic jerseys
Wholesale nfl jersey
wholesale nhl jerseys
wholesale hockey jerseys
discount nhl jerseys
Wholesale nba jerseys
wholesale basketball jerseys
discount nba jerseys
Wholesale mlb jerseys
wholesale baseball jerseys
discount mlb jerseys
Wholesale NCAA Jerseys
Wholesale Custom Jersey
Kobe Bryant Jersey
Air Jordan Shoes
Wholesale air jordans
Replica Air Jordan
Cheap Air Jordan
Packers Jerseys
Green Bay Packers Jersey
Posted by: fiona | 06/25/2011 at 09:40 AM
Wow, Great post,Nice work, I would like to read your blog every day Thanks
Posted by: Networking solutions | 06/30/2011 at 02:48 PM
Thanks for the write up! This is really some great stuff here! Wishing for a following post for the similar subject.
Posted by: Nike Pippen | 07/20/2011 at 02:48 AM
Great tips, I would like to join your blog anyway,
Posted by: ザイロリック | 07/20/2011 at 12:47 PM
There are may person searching about that now they will find enough resources by your post..
Posted by: SEO Services India | 07/20/2011 at 02:55 PM
Breataine agus cead a tethering a dhéanamh suas an difríocht!
Posted by: Generic Viagra | 09/09/2011 at 01:47 PM
Hi! I came across your site accidentally today, but am really pleased that we did! It’s not only entertaining, but in addition straightforward to utilize weighed against lots that I’ve viewed
Houton Home Security
Posted by: Houston Home Security | 10/31/2011 at 08:17 AM
Lately I came to your website and have been reading along. I thought I would leave my initial comment. Keep writing, because your posts are impressive!
Posted by: Smart Home | 11/17/2011 at 09:49 AM