Constructors & Destructors?
-
hi all, so I know what the constructor does. Is there also a destructor for C# classes? and is it invoked when I do a set myobject = nothing ? also, if Im creating one of those classes that you DONT have to instantiate (what are they called again?), how does the constructor and destructor get called? and is it a good idea to put stuff in the constructor and destructor in those type of classes? thanks Moazzam
-
hi all, so I know what the constructor does. Is there also a destructor for C# classes? and is it invoked when I do a set myobject = nothing ? also, if Im creating one of those classes that you DONT have to instantiate (what are they called again?), how does the constructor and destructor get called? and is it a good idea to put stuff in the constructor and destructor in those type of classes? thanks Moazzam
moazzamahmed wrote:
Is there also a destructor for C# classes?
Yes, it's called Finalize.
moazzamahmed wrote:
and is it invoked when I do a set myobject = nothing
No. It's invoked when the garbage collector is about to remove the object. If you need to do any cleanup in your object, you should inherit the IDisposable interface and implement the Dispose method.
moazzamahmed wrote:
also, if Im creating one of those classes that you DONT have to instantiate (what are they called again?), how does the constructor and destructor get called?
Do you mean a static class? As you don't instantiate it, the constructor or destructor aren't used. Actually, you should declare a private constructor so that noone can create an instance of the class. However, you can also declare a static constructor for the class, that will be called once when the library is loaded. --- b { font-weight: normal; }
-
moazzamahmed wrote:
Is there also a destructor for C# classes?
Yes, it's called Finalize.
moazzamahmed wrote:
and is it invoked when I do a set myobject = nothing
No. It's invoked when the garbage collector is about to remove the object. If you need to do any cleanup in your object, you should inherit the IDisposable interface and implement the Dispose method.
moazzamahmed wrote:
also, if Im creating one of those classes that you DONT have to instantiate (what are they called again?), how does the constructor and destructor get called?
Do you mean a static class? As you don't instantiate it, the constructor or destructor aren't used. Actually, you should declare a private constructor so that noone can create an instance of the class. However, you can also declare a static constructor for the class, that will be called once when the library is loaded. --- b { font-weight: normal; }
fabulous! thanks Guffa. so whats the use of static classes over non-static? which ones should we use for what TYPE of object? whats better for speed and performance? I was thinking, if a class is basically just utilities, it should be static. But if its something that has properties etc. e.g., a "contact", its should be instantiated. Am I on the right track? thanks again.
-
fabulous! thanks Guffa. so whats the use of static classes over non-static? which ones should we use for what TYPE of object? whats better for speed and performance? I was thinking, if a class is basically just utilities, it should be static. But if its something that has properties etc. e.g., a "contact", its should be instantiated. Am I on the right track? thanks again.
-
fabulous! thanks Guffa. so whats the use of static classes over non-static? which ones should we use for what TYPE of object? whats better for speed and performance? I was thinking, if a class is basically just utilities, it should be static. But if its something that has properties etc. e.g., a "contact", its should be instantiated. Am I on the right track? thanks again.
moazzamahmed wrote:
so whats the use of static classes over non-static? which ones should we use for what TYPE of object?
Use a static class when you don't need to create objects from the class. Generally static classes don't contain any data at all, or if they do, it's data that is common to the entire application.
moazzamahmed wrote:
whats better for speed and performance?
Calling a static method means that a reference to the object isn't included in the call, saving one instruction. This difference is hardly ever anything that you have to be concerned about, though. Concentrate on what gives the best structure to the code, and start thinking about optimizing when you run into performance problems. But even then, there are a lot of other things that are likely to boost performance a lot more than using static methods instead of non-static. --- b { font-weight: normal; }