"Ant" <An*@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
Hi
I'm quite new to C#. I've been playing around with variables declared
outside the scope of a method with & without the static keyword. What
difference does declaring variables in either fashion make? For that
matter,
why must you declare methods with the static keyword?
Thanks for any ideas
Ant
I'll get this started with what will probably be an inadequate explanation.
A static datamember of a class (declared as you said, outside a method's
code block), exists from the time the class definition is available to your
application throughout its life, and only one instance of the static field
is created no matter how many objects of the class are created. The static
datamember is independent of objects created. Contrast this behavior with
that of instance datamembers (declared without the static keyword) which
exist only in the context of an object created from the class. Each object
gets its own copy of each instance datamember. Zero instances of instance
datamembers exist until objects are created.
Static methods are normally created to manipulate static datamenbers in
situations where there are no objects of the class available, because
instance methods, although they can access static datamembers, cannot be
used unless you have an object to qualify the call.
Knowing the group here, you'll get better explanations, so hang in there.
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.