I was reading for this question: Difference between destructor, dispose and finalize method
I've read that the destructor was used to delete unmanaged resources, but after running a little test :
using System;
namespace Tests
{
class ImABastardException : Exception
{
public ImABastardException() : base() { }
}
class Mouarf
{
public Mouarf()
{
Console.WriteLine("Constructor");
throw new ImABastardException();
}
~Mouarf()
{
Console.WriteLine("Destructor");
}
}
class Program
{
private static void Func()
{
Console.WriteLine("Before");
try
{
Console.WriteLine("Try");
Mouarf m = new Mouarf();
}
catch (ImABastardException)
{
Console.WriteLine("Catch");
}
finally
{
Console.WriteLine("Finally");
}
Console.WriteLine("After");
}
static void Main(string[] args)
{
Console.WriteLine("MainBefore");
Func();
Console.WriteLine("MainAfter");
Console.ReadKey();
}
}
}
The result was :
MainBefore
Before
Try
Constructor
Catch
Finally
After
MainAfter
(I push enter or another key)
Destructor
I'm afraid that the destructor isn't called when I want (at the end of its range)..
I actually want to create a simple class :
public unsafe class StringArray
{
private sbyte** pointer;
private int count;
public StringArray(string[] array)
{
count = array.Length;
pointer = (sbyte**)Memory.Alloc(count * sizeof(sbyte*));
for (int i = 0; i < count; i++)
{
fixed (byte* p = Encoding.ASCII.GetBytes(array[i]))
{
pointer[i] = (sbyte*)p;
}
}
}
~StringArray()
{
Memory.Free(pointer);
}
public sbyte** Pointer
{
get
{
return pointer;
}
}
public int Count
{
get
{
return count;
}
}
}
public unsafe class Memory
{
// see : https://msdn.microsoft.com/fr-fr/library/aa664786%28v=vs.71%29.aspx
}
But I think it would be better to use Dispose for unmanaged resources, what do you think?
Thanks!
Sylafrs.
Edit:
I've had asked that question as an answer to the topic I've given the link above;
Lasse V. Karlsen answered:
"As a quick comment to your actual question, I would implement IDisposable on that type, and have the finalizer call Dispose(false);"
So I've tested:
using System;
namespace Tests
{
class ImABastardException : Exception
{
public ImABastardException() : base() { }
}
class Mouarf: IDisposable
{
// Flag: Has Dispose already been called?
bool disposed = false;
public Mouarf()
{
Console.WriteLine("Constructor");
throw new ImABastardException();
}
~Mouarf()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
Console.WriteLine("Disposing managed objects");
}
Console.WriteLine("Disposing unmanaged objects");
disposed = true;
}
}
class Program
{
private static void Func()
{
Console.WriteLine("Before");
Mouarf m = null;
try
{
Console.WriteLine("Try");
using (m = new Mouarf())
{
;
}
}
catch (ImABastardException)
{
Console.WriteLine("Catch");
}
finally
{
Console.WriteLine("Finally");
}
Console.WriteLine("After");
}
static void Main(string[] args)
{
Console.WriteLine("MainBefore");
Func();
Console.WriteLine("MainAfter");
Console.ReadKey();
}
}
}
IDisposable doesn't work too.. (the "Disposing unmanaged objects" message was written at the program's end)
I guess the problem comes from the constructor.. Must I use a method to do risky tasks?