ianinspain
asked on
500pts: Which methods to implement in a class by default i.e ToString()?
Hi there,
I was looking for some b ackground info on what methods i should be implementing by default in my classes (or overriding)...
I remember seeing a document once outlining best practises ... what to override, what interfaces to use if you wish to use a foreach on your class etc... but i just can't find it...
I was wondering if anyone has any recommendations of what methods / properties I should be implementing /overriding by defautl?
Also currently i am always using and awful lot 'private' access modifiers on classes... should i really be using protected so it can be picked up in another class/derived class... i suppose this depends,, but it was just concerning me that all my classes contain private members etc
Look forward to any information..
Thanks in advance
Ian
I was looking for some b ackground info on what methods i should be implementing by default in my classes (or overriding)...
I remember seeing a document once outlining best practises ... what to override, what interfaces to use if you wish to use a foreach on your class etc... but i just can't find it...
I was wondering if anyone has any recommendations of what methods / properties I should be implementing /overriding by defautl?
Also currently i am always using and awful lot 'private' access modifiers on classes... should i really be using protected so it can be picked up in another class/derived class... i suppose this depends,, but it was just concerning me that all my classes contain private members etc
Look forward to any information..
Thanks in advance
Ian
Basicly you don't need to override any class by default. If you aren't using the ToString() method, don't bother overriding it.
For the rest it's basicly what AlexFM says. However I would add that you should implement IDisposable also when the object can contain a lot of data. That way it gets cleaned a bit quicker when you call the Dispose() method.
For the rest it's basicly what AlexFM says. However I would add that you should implement IDisposable also when the object can contain a lot of data. That way it gets cleaned a bit quicker when you call the Dispose() method.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Let's say that array belongs to class instance. If class instance has 0 reference counter, it becomes candidate for garbage collecting. At the same time, this array becomes candidate for garbage collecting as well, if it is not referenced somewhere outside of class. Array has reference counter 1, but it is referenced only from "dead" instance, so this array is still subject of collecting. Setting array reference to null inside of class reduces array reference counter to 0, but this doesn't change anything.
It is interesting to write simple program which tests this, I will do this some time later. However, this discussion is out of scope of original question.
It is interesting to write simple program which tests this, I will do this some time later. However, this discussion is out of scope of original question.
ASKER
excellent thanks... splitting points..
Ian
Ian
All other depends on your requirements. If class should be serializable, implement ISerializable interface. If class is some collection wrapper, implement INumerable interface. If class allocates some unmanaged resources, or has IDisposable members, implement IDisposable interface.
If you don't want this class to be inherited, make it sealed, and make all non-public method private.
If you want to allow inheritance, don't use sealed. Decide, which methods you want to be inherited, and make them protected.