Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

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

"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
Posted
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.
C#
Posted

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!

Howzit??
  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...