Declaring with static

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Ant said:
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.
 
Hi Ant,

Static member variables exist at class scope, while non-static
variables exist at instance scope. What this means is that if you
have declared:

class Ant
{
static int x = 0;
}

then all object instances in the Ant class will be accessing the same
variable x. So:

Ant a;
Ant b;
a.x = 3;

then b.x will be 3. If x were not defined as static, then each
instance has its own copy of x, and b.x would be 0 (and a.x would still
be 3). This is used to allow all instances of a class to share some
information. Think of them as class-wide global variables.

As for static on methods, you put them on methods that you want to
be able to call without an instance of that class. For example, if I
declare

class Ant
{
public static void Hello1() {...}
public void Hello2() {...}
}

then I can call Ant.Hello1(), but not Ant.Hello2(). Hello2()
requires that I have an instance, so Ant ant; ant.Hello2(); would
work, whereas with Hello1() I can invoke the method using the class
name. This is usually used either because one wants a user to be able
to change static variables, hence a static function fits the bill, or
you are grouping some functions into a class based on semantic
similarity, and an instance isn't really necessary. For instance,
String.Format() semantically belongs in the String class, but doesn't
require an instance, so it is static.

Hope this helps,
Harold
 
Static member variables exist at class scope, while non-static
variables exist at instance scope. What this means is that if you
have declared:

class Ant
{
static int x = 0;
}

then all object instances in the Ant class will be accessing the same
variable x.

More than that - there don't have to be *any* instances of the Ant
class in order to use static members. Static members are associated
with the type rather than any particular instance of the type.
So:

Ant a;
Ant b;
a.x = 3;

then b.x will be 3.

Fortunately, the above won't compile. C# took the wise decision of
forbidding access to static members via instances.

<snip>
 
Hi Ant,

The scope of the static members are at class level but the scope of the
reference members are at object level.
 
Back
Top