Windows Develop Bookmark and Share   
 index > Windows Forms General > private static methods vs private non-static methods?
 

private static methods vs private non-static methods?

Could someone please tell the difference between private static methodsand private non-static methods. I know that astatic method is created only once per class rather than in every object based on that class, and we can access a public static class member without creating an instance of the class. However, I am not quite sure when we shoulduse private static methods.

Thanks.

tdcntt  Sunday, January 14, 2007 9:34 PM
It is easier to do it the other way around. No method *has* to be static. It *could* be static if it doesn't use any other non-static member of the class.
nobugz  Monday, January 15, 2007 6:38 AM
You can use a static method when its outcome doesn't depend on the values of the class instance. To use the worn MSFT example, the Dinosaur class can represent various species of dinosaurs. The EatMammal() method could be static, the EatTreeLeaves() couldn't. The rulez change when you change the hierarchy. If your base class is Animal, you'll need to change EatMammal from static to virtual. Assuming that an oyster is incapable of catching a cow...

Sorry to be flippant, I had a bit too much fun with the examples. You actually have a choice between static, non-virtual and virtual. The non-virtual implementation of a method is appropriate when you are not going to derive another class from yours. The appropriate keyword to use in that case is "sealed".
nobugz  Sunday, January 14, 2007 11:10 PM

Thanks nobugsz, but I am still confused and still don't quite to understand the difference between a private static method and private non-static method. Could you give me an example where a private method has to be static? (If that private method is non static, the program doesn't work as expected.)

Thanks.

tdcntt  Monday, January 15, 2007 6:07 AM
It is easier to do it the other way around. No method *has* to be static. It *could* be static if it doesn't use any other non-static member of the class.
nobugz  Monday, January 15, 2007 6:38 AM

You can use google to search for other answers

Custom Search

More Threads

• Question about copy data from multiple array to one-dimension array, e.g. htt = (Int32[]) tt.getValue(1);
• System.Windows.Forms.Timer not firing
• Locking down the desktop
• Problem with a list.
• Can you tell me where can I find the best custom control collections for C#?
• Custom Control that handles Format/Parse internally
• turn off GroupBox border
• DataGridViewComboBoxColumn how to set default index?
• Design Buttons To be used in c# other than the original ones
• Displaying selected items in an unfocused ListView