quick Static Method question

  • Thread starter Thread starter z_learning_tester
  • Start date Start date
Z

z_learning_tester

Hello,
I am new to C# and have a quick question.
Any replies much appreciated ;-)

It is regarding static methods...
Are they public by default?
I mean it seems like for them to be usable, they have to be public right?

EG. Below you can't call the method by saying Point.ObjectCount() unless
ObjectCount is public right? So this call would fail ?

class Point
{ ...
static int ObjectCount ()
{...
}
}

Thanks again-
Jeff
 
z_learning_tester said:
I am new to C# and have a quick question.
Any replies much appreciated ;-)

It is regarding static methods...
Are they public by default?

No, it's private by default. The rule is that everything's as
restrictive as it can be by default.
I mean it seems like for them to be usable, they have to be public right?

Only if you need to call them from other assemblies. Sometimes you may
want static methods which are private, sometimes you may want internal
ones, and sometimes you may want public ones.
EG. Below you can't call the method by saying Point.ObjectCount() unless
ObjectCount is public right? So this call would fail ?

class Point
{ ...
static int ObjectCount ()
{...
}
}

Well, if you just declare "class Point" it'll be internal to the
assembly anyway, so making a method in it public isn't terribly useful
for the most part. As it stands above, you could only call ObjectCount
within the class itself.
 
Great! Thanks for the response.
I'm still struggling to see the point of having a private static method.
I mean you use static so you can access it outside the assembly without
creating an instance of the object right?

So instead of private static, why not just say private? Is there a
difference if its private?
 
z_learning_tester said:
Great! Thanks for the response.
I'm still struggling to see the point of having a private static method.

To call it from within the same class, but not allow people to call it
from outside the class.
I mean you use static so you can access it outside the assembly without
creating an instance of the object right?

No. You use static so you can access it without creating an instance of
the object. It has nothing to do with accessibility.
So instead of private static, why not just say private? Is there a
difference if its private?

Static/instance and accessibility are entirely separate concepts.
Static/instance determines whether it operates on an instance or
whether it sort of applies in a type-wide way. Accessibility says where
you can call it from.
 
Hmm, very interesting.
So if static static just determines how you call the method (by not having
to create an instance of the object to call it on, and rather to call it
directly on the class) then it would seem preferable to use static whenever
possible. No instance of the object = less memory used. True? After all it
seems just as easy to code it either way...
 
z_learning_tester said:
Hmm, very interesting.
So if static static just determines how you call the method (by not having
to create an instance of the object to call it on, and rather to call it
directly on the class) then it would seem preferable to use static whenever
possible. No instance of the object = less memory used. True? After all it
seems just as easy to code it either way...

You shouldn't think of it that way. Think of whether it's *naturally*
an instance method or not - does it operate on a particular instance,
or is it general, either only operating on static variables, or not
requiring any context at all.
 
Wow, good insight.
I wish my book explained it that way...
But then it is a beginner book and more focussed on the syntax than when and
why to use it.

Thanks again!

Jeff
 
Back
Top