Click here to Skip to main content
15,914,016 members
Home / Discussions / C#
   

C#

 
GeneralRe: find file in web Pin
Manas Bhardwaj16-May-09 6:17
professionalManas Bhardwaj16-May-09 6:17 
GeneralRe: find file in web Pin
michaelgr116-May-09 6:35
michaelgr116-May-09 6:35 
AnswerRe: find file in web Pin
MumbleB16-May-09 6:45
MumbleB16-May-09 6:45 
GeneralRe: find file in web Pin
michaelgr116-May-09 7:09
michaelgr116-May-09 7:09 
GeneralRe: find file in web Pin
michaelgr116-May-09 8:28
michaelgr116-May-09 8:28 
AnswerRe: find file in web Pin
S. Senthil Kumar16-May-09 9:11
S. Senthil Kumar16-May-09 9:11 
QuestionC++ virtual destructor in C#? Why not? Pin
devvvy16-May-09 2:23
devvvy16-May-09 2:23 
AnswerRe: C++ virtual destructor in C#? Why not? Pin
S. Senthil Kumar16-May-09 4:45
S. Senthil Kumar16-May-09 4:45 
devvvy wrote:
Should we make Base.Finalize (~Base really in C# syntax) virtual? Should we implement Derived.Finalize (~Derived in C# syntax)?


It's virtual, and the C# compiler doesn't allow you the option of making it non-virtual. Take a look at the generated IL if you don't believe it. For the below C# code.

class A
{
    ~A() { Console.WriteLine("A"); }
}

class B : A
{
    ~B() { Console.WriteLine("B"); }
}


Here's how the IL looks.

.class auto ansi nested private beforefieldinit A
    extends [mscorlib]System.Object
{
    .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
    {
    }

    .method family hidebysig virtual instance void Finalize() cil managed
    {
    }

}


Note that Finalize is indeed virtual.

devvvy wrote:
C++ destructor is C# destructor (or Finalizer) - now SINCE we really want to release resource (expensive ones in particular) in Dispose (deterministically, unlike ~Derived or ~Base), what really I'm asking is, why not "Virtual destructor"


The C# destructor (Finalizer) is not the equivalent of a destructor in C++, it's unfortunate that the syntax for both of them is the same. You're right about Dispose not being automatically chained up though - I guess that's because it's not really a part of the CLR. C# (and the BCL) provide the functionality through the IDisposable interface, and chaining up for Dispose would mean that other interface methods of any interface should also chain up. There's also the possibility that the base class doesn't actually implement IDisposable as well.

devvvy wrote:
In C++, if you declared destructor virtual, and "delete oDerived", BOTH destructor from Base and Derived are invoked (~Derived then ~Base)


Again, it happens for finalizers in .NET, but not for IDisposable. Again, it's because IDisposable is not an intrinsic part of the CLR, and not all objects implement IDisposable.

C++/CLI actually provides deterministic "dispose" functionality.

ref class A : public IDisposable
{
   !A() { Console::WriteLine("Dispose for A"};
};

void Main()
{
   {
   A a; // Create object with deterministic dispose semantics.
   } // !A() will be called.

   A ^a = gcnew A(); // No deterministic dispose.
}


Regards
Senthil [MVP - Visual C#]
_____________________________
My Home Page |My Blog | My Articles | My Flickr | WinMacro

GeneralRe: C++ virtual destructor in C#? Why not? Pin
devvvy16-May-09 5:37
devvvy16-May-09 5:37 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
S. Senthil Kumar16-May-09 9:01
S. Senthil Kumar16-May-09 9:01 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
N a v a n e e t h16-May-09 7:30
N a v a n e e t h16-May-09 7:30 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
S. Senthil Kumar16-May-09 8:47
S. Senthil Kumar16-May-09 8:47 
AnswerRe: C++ virtual destructor in C#? Why not? Pin
Mark Salsbery16-May-09 6:41
Mark Salsbery16-May-09 6:41 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
devvvy16-May-09 8:02
devvvy16-May-09 8:02 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
Mark Salsbery16-May-09 8:08
Mark Salsbery16-May-09 8:08 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
devvvy16-May-09 8:10
devvvy16-May-09 8:10 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
Mark Salsbery16-May-09 8:17
Mark Salsbery16-May-09 8:17 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
S. Senthil Kumar16-May-09 9:08
S. Senthil Kumar16-May-09 9:08 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
Mark Salsbery16-May-09 9:20
Mark Salsbery16-May-09 9:20 
AnswerRe: C++ virtual destructor in C#? Why not? Pin
Guffa16-May-09 16:46
Guffa16-May-09 16:46 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
devvvy16-May-09 17:04
devvvy16-May-09 17:04 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
Guffa22-May-09 13:27
Guffa22-May-09 13:27 
QuestionUse methods from another form Pin
michaelgr116-May-09 2:08
michaelgr116-May-09 2:08 
AnswerRe: Use methods from another form Pin
OriginalGriff16-May-09 2:39
mveOriginalGriff16-May-09 2:39 
AnswerRe: Use methods from another form Pin
michaelgr116-May-09 3:03
michaelgr116-May-09 3:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.