Solved

500pts: Which methods to implement in a class by default i.e ToString()?

Posted on 2006-11-29
7
178 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:ianinspain
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 18035294
By default, ToString returns type name. If you want to replace this with something helpful, override ToString.
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.
0
 
LVL 14

Expert Comment

by:existenz2
ID: 18041048
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.
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 150 total points
ID: 18041479
about private, protected etc. modifiers.

There are good reasons to use them all, again, it depends on your needs and requirements. It's a common practice to have private fields and access them through public properties. If you need to extend some classes you still may have private fieldsin them and access them through public properties. In some other situations protected comes quite handy...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 18043326
>> you should implement IDisposable also when the object can contain a lot of data.
There are two reasons to implement IDisposable: class allocates unmanaged resources, or contains disposable members. Lot of data is not a reason. Dispose method doesn't release managed resources, even if there is a lot of them.
0
 
LVL 14

Assisted Solution

by:existenz2
existenz2 earned 150 total points
ID: 18043469
Alex> I agree, wasn't really clear about that. However if you have a very big byte array in your class, you most likely do want to do "data = null;" in the overriden Dispose() method. The data won't be freed right away, but it will get the "clean" flag faster and the Garbage Collector will free it up faster then normally. That's what I meant to say.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 18043569
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.
0
 

Author Comment

by:ianinspain
ID: 18075781
excellent thanks... splitting points..

Ian
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 53
Two different visual studio versions 3 21
SQL Query Producing decimal places when it shouldn;t be 8 35
Reset asp.net login password 4 25
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now