Static methods belong to the class. Non-static methods belong to an instance
of a class. So, to call a non-static method, you must create an instance of
the class first. For example,
public class Foo {
public Foo() {}
public void InstanceMethod() {}
public static void ClassMethod() {}
}
To call an instance method, I first need an instance. I create an instance
of a class by creating one with new. Then I can call its method. For
example:
Foo myFoo = new Foo() // create an instance
myFoo.InstanceMethod();
To call a class method, I do not create an instance. I just specify the
class and I can use it. It is globaly available through the class name. For
example:
Foo.ClassMethod();
Note: This brings up an interesting point. Static methods may not access
data in the class except for static data. Since there is no instance of a
class, the data would not exist.
Statics are used a lot for utility classes where you have serveral related
functions. The Math class in .Net is a good example. Another use is for
Factory methods. See the Fatory Method design pattern.
Hope this helps....
Frisky
"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Greetings,
I have a "newbie" question relating to C#. I am still trying to
understand the difference between a static and a non-static method
(particularly when it is called), and was wondering if anyone could
point me to some additional resources? If I see a method call, how can
I be sure that it is Static (or Non-Static)?
Thanks in advance!
*** Sent via Developersdex http://www.developersdex.com ***