aewarnick Posted March 4, 2003 Posted March 4, 2003 Are there any disadvantages to making static methods for classes instead of just public? Quote C#
*Experts* Nerseus Posted March 4, 2003 *Experts* Posted March 4, 2003 Not so much disadvantages as just differences. A static method is like a global function. A public method is intended to do some action on a specific instance of a class. So a Car object might have a method named Start() but you wouldn't make it static because you only start a specific car (for example). The DateTime object has a static method IsLeapYear(int year) that returns true or false whether the year is a leap year. It's static because you don't want to have to create a DateTime object just to call that function. Whether your objects have static or just public methods is entirely up to you - it's just whatever makes sense. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
aewarnick Posted March 4, 2003 Author Posted March 4, 2003 Static methods are more convenient and eisier to use but I have seen that when I try to create a thread for the method I cannot. Or at least I don't know how. I will post a new post right now about threads. Quote C#
Cywizz Posted March 4, 2003 Posted March 4, 2003 Nerseus... This brings me to questions that bugged me for a while: How does the garbage collector handle static vars/methods? Looking at memory resources, is it good or bad to use statics? Any guidelines? Thanks! Quote Howzit??
*Experts* Nerseus Posted March 4, 2003 *Experts* Posted March 4, 2003 Again, it's not good or bad to do anything if it's needed. But you probably don't need very many static variables, in general (I've never had the need for more than 2 or 3). Static methods only take up the memory for the compiled instructions of course, just like any other code. I don't remember offhand how the garbage collector treats static variables - I would imagine it treats them like any other chunk of memory. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
aewarnick Posted March 4, 2003 Author Posted March 4, 2003 Thank you Nerseus. You were very helpful. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.