|
Thanks N a v a n e e t h,
I have read through the article, looks great and answered most of my questions. One question which is not answered is what is the relationship between Finalize method and destructor?
Finalize will call destructor or vice versa? Or no relationship? GC will call Finalize, but I am not sure whether GC will call destructor as well?
regards,
George
|
|
|
|
|
George_George wrote: what is the relationship between Finalize method and destructor?
As far as the CLR is concerned, in C# something looks like a destructor is finalize method. System.Object has a Finalize() method, but C# won't allow you to override this. In C#, destructor is called as finalize method. To make it more clear look at the below class which uses a destructor and check the IL generated code
class Program
{
static void Main(string[] args)
{
}
~Program()
{
}
}
.class private auto ansi beforefieldinit Program
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}
.method family hidebysig virtual instance void Finalize() cil managed
{
}
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
}
} In the IL code you can see the Finalize() method. So C# compiler converted destructor to this Finalize() method. This will be called by garbage collector.
Considering performance, implementing destructor is costly. GC will maintain a separate queue for items that need finalization. So you should implement the Dispose() pattern and clean all your resources. Also call GC.SuppressFinalize() from dispose which will suppress the finalization.
Hope things are clear now
|
|
|
|
|
Thanks N a v a n e e t h,
1.
Which tool are you using to check IL code?
2.
N a v a n e e t h wrote: GC will maintain a separate queue for items that need finalization. So you should implement the Dispose() pattern and clean all your resources. Also call GC.SuppressFinalize() from dispose which will suppress the finalization.
I do not understand why using Dispose is faster than using GC Finalize? GC may have some smarter ways to treat all the to be finalized resources altogether?
regards,
George
|
|
|
|
|
George_George wrote: Which tool are you using to check IL code?
I use reflector[^]. It's a great tool
George_George wrote: GC may have some smarter ways to treat all the to be finalized resources altogether?
Yes, GC is smart. But you can't predict when GC will collect the resources. So implementing your own disposal methods, you are ensuring that all the unmanaged resources are deallocated once you finish with particular object. You won't be waiting for GC to clear that. Also classes that have finalize method, GC need two cycles to reclaim the memory fully. So it's an overhead.
|
|
|
|
|
Thanks N a v a n e e t h,
I watched it is not a Microsoft tool. So, Microsoft does not provide a similar tool?
regards,
George
|
|
|
|
|
Nope . But it is written by a Microsoft employee. It's must tool for every .NET programmers toolbox.
PS : I like the way you ask questions. It's clear and up to the point. Keep it up.
|
|
|
|
|
Thanks for your help, N a v a n e e t h!
I am previously a C++/Java developer, and I am new to C#. Happy to learn from you, especially for what are not given by MSDN.
regards,
George
|
|
|
|
|
|
Thanks gareth111,
I have read through the article, looks great and answered most of my questions. One question which is not answered is what is the relationship between Finalize method and destructor?
Finalize will call destructor or vice versa? Or no relationship? GC will call Finalize, but I am not sure whether GC will call destructor as well?
regards,
George
|
|
|
|
|
Hi guys.
I want to play video files (*.mpeg, *.avi, ...) in my application with C# and DirectX . but I do not know how I do it?
Can you help me ? thanks.
www.mds-soft.persianblog.ir
|
|
|
|
|
You use the windows media player control, or managed direct show ( which is in the platform SDK nowadays ) to play video files. A cursory search of the web would give you tons of samples
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
ok, thanks.
Only God
|
|
|
|
|
Hello everyone,
1.
C# has integer types, int, int32, int64 and long. In my understanding, int32 is always 32-bit, int64 and long are always 64-bit.
My understanding correct?
2.
How about int? Is it 32-bit or 64-bit or depends on how we build application (build as 32-bit? build as 64-bit or any CPU?)
thanks in advance,
George
|
|
|
|
|
George_George wrote: C# has integer types, int, int32, int64 and long. In my understanding, int32 is always 32-bit, int64 and long are always 64-bit.
Yes, Int32 represents a 32 bit signed integer, and int64 a 64 bit signed integer.
George_George wrote: How about int? Is it 32-bit or 64-bit or depends on how we build application (build as 32-bit? build as 64-bit or any CPU?)
It represents 32bit by default.
|
|
|
|
|
Thanks N a v a n e e t h!
regards,
George
|
|
|
|
|
|
Cool, your reply is clear, Gareth!
regards,
George
|
|
|
|
|
All numbers are fixed sizes except IntPtr.
|
|
|
|
|
Cool, leppie!
Question answered.
regards,
George
|
|
|
|
|
Hi,
How to move all items(not selected) in a listbox to a string array??
i want to move items in a listbox to a string array.How can i do that??
Plz help..
regards,
syamoooo
|
|
|
|
|
Use a foreach loop on the items in your listbox and add the ones that are not selected to a List<string> (provided you're using .NET 2.0 and above)
He who makes a beast out of himself gets rid of the pain of being a man
|
|
|
|
|
Hi all,
In my application i have 5 forms. I would like to store some value in global varialble which can access by any forms in my app.I have try to create a get/set class and i declare a public object in form let said form A. But i can't access the object in form B. My example coding as below:
Class GetSet:
private String _name;<br />
public String name<br />
{<br />
get{return _name;}<br />
set{_name = value;}<br />
}
FormA:
public partial class FrmA : Form<br />
{<br />
GetSet gs;<br />
public FrmMain()<br />
{<br />
gs = new GetSet();<br />
gs.name = "cocoon";<br />
}<br />
<br />
}
FormB:
public partial class FrmB : Form<br />
{<br />
GetSet gs;<br />
public FrmMain()<br />
{<br />
console.write(gs.name);<br />
}<br />
<br />
}
Can anyone guide me to slove this out.
thanks in advance
regards
cocoon
|
|
|
|
|
You have to rethink your design.
cocoonwls wrote: How to create global variable
You can't. However, you can create a static class which is quite similar to what you are asking for. There are plenty of examples on Google.
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
The way to do this, is to use delegates, or create a class with static properties. If you thought that FrmB would do anything but crash, or had any chance of seeing hte value 'cocoon', you need to do some serious reading on OO, because you've misunderstood quite a bit.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Vikram and Christian,
Thanks for your reply. I have read through the static class and it useful to me. Then, Christian Graus, can you please explan a bit more to me why would you said i am misunderstood OO concept?
Thanks in advance.
regards
cocoon
|
|
|
|