Question about 'public' and 'public static'

S

Schizoid Man

Why do I have to declare a 'public' function as 'static' in a public
class in order to make it visible from another class?

In this regard I don't understand how public functions without the
static declaration are different from private functions, because I
cannot call these method from outside the class that those methods are
declared in.

All classes refer to the same namespace.

Thanks,
Schiz
 
J

Jon Skeet [C# MVP]

Schizoid Man said:
Why do I have to declare a 'public' function as 'static' in a public
class in order to make it visible from another class?

In this regard I don't understand how public functions without the
static declaration are different from private functions, because I
cannot call these method from outside the class that those methods are
declared in.

All classes refer to the same namespace.

Whether a member (method, property, variable etc) is static or not has
nothing to do with whether or not it's public. It's to do with whether
it's conceptually something to do with the type as a whole, or an
individual instance of the type.

It's not a hugely easy topic to describe quickly in a newsgroup post,
but I suggest you consult a book or tutorial about it - it's a key
concept in .NET.
 
P

Peter Morris

Why do I have to declare a 'public' function as 'static' in a public class
in order to make it visible from another class?

You don't, but you do have to create an instance of the class to see the
public method.

public class MyPublicClass
{
public static void PublicStaticMethod() { }
public void PublicInstanceMethod() { }
}


in another class.....

MyPublicClass. (code insight will only show PublicStaticMethod in the list)

MyPublicClass instance = new MyPublicClass();
instance. (code insight will show PublicInstanceMethod)


Pete
 
B

Bob Powell [MVP]

You seem to have confused the intent of the public versus private accessor
and the static keyword.

Static defines a method, property or class that is associated with a
particular type but which does not require access to the instance members of
the type. A static method can be called using the form
MyType.MyStaticMethod. The static method must be public in order to be used
from another class.

A non static public method requires an instance of the class:

MyType mt=new MyType();
mt.MyPublicMethod();

A static method may not access any instance data or other instance methods
of a class so this will not work:

class MyClass
{
int t;

public static void ChangeT()
{
t=10;
}
}

I hope this clarifies somewhat but as John suggests you should probably be
looking at a textbook.
--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
S

Schizoid Man

Bob said:
You seem to have confused the intent of the public versus private
accessor and the static keyword.

Static defines a method, property or class that is associated with a
particular type but which does not require access to the instance
members of the type. A static method can be called using the form
MyType.MyStaticMethod. The static method must be public in order to be
used from another class.

A non static public method requires an instance of the class:

MyType mt=new MyType();
mt.MyPublicMethod();

A static method may not access any instance data or other instance
methods of a class so this will not work:

class MyClass
{
int t;

public static void ChangeT()
{
t=10;
}
}

I hope this clarifies somewhat but as John suggests you should probably
be looking at a textbook.

Thank you all for the replies - I really appreciate it. I have a C++
background and am preparing for an interview.

Suppose I am writing a general utility 'class' (simple functions like
Max, Min, Average, etc) that does not need access to instance members
because each function is self contained.

Wouldn't it be inefficient for me to instantiate the Utility class and
then class the appropriate Utility.Method rather than applying the
method straight away without the creating the instance?

I could achieve this in C++ by declaring the methods outside of a class
in the header and then include the appropriate header in the source file.

What would be the equivalent in C#? Interpreting Bob's description,
declaring the appropriate methods as static would be the way to go.

Thanks again.
 
B

Bob Powell [MVP]

The case you cite this is a valid use of the static method. System.Math is a
prime example of this. Each method is self contained, works on the
parameters supplied and returns some value. System.Math cannot be
instantiated and encapsulates no data.

Take a look at the method prototypes for Math or perhaps peek inside with
Reflector.

Be careful when designing your architecture that you don't begin using the
static system as a substitute for global methods and variables. This is very
bad pracatice.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
J

Jon Skeet [C# MVP]

Schizoid Man said:
Thank you all for the replies - I really appreciate it. I have a C++
background and am preparing for an interview.

Suppose I am writing a general utility 'class' (simple functions like
Max, Min, Average, etc) that does not need access to instance members
because each function is self contained.

Right. In that case, not only could all the methods be static, but you
might want to mark the whole *class* as being static. That would
prevent it being instantiated in the first place.
 
S

Schizoid Man

Jon said:
Right. In that case, not only could all the methods be static, but you
might want to mark the whole *class* as being static. That would
prevent it being instantiated in the first place.

Thank you for the answers.
 
S

Schizoid Man

Jon said:
Right. In that case, not only could all the methods be static, but you
might want to mark the whole *class* as being static. That would
prevent it being instantiated in the first place.

Hi Jon, Bob,

Just to continue this thread for a moment...

Suppose I'm writing a class for a statistical distribution. I write the
appropriate constructors, properties and methods (in this case a simple
mean, or average). In this case I would just use the declaration 'public
double', and its usage would be:

StatDist test = new StatDist();
double Avg = test.Average();

However, if I want to also write a general purpose Average function in
the same class, I'd want to overload the function using 'public static
double' so that its usage would be:

double x[] = { ... };
double Avg = StatDist.Average(x);

Is it a good (or bad) idea to overload the function and make one of them
static all in the same class?

Regards,
Schiz
 
S

Schizoid Man

Jon said:
Right. In that case, not only could all the methods be static, but you
might want to mark the whole *class* as being static. That would
prevent it being instantiated in the first place.

Hi Jon, Bob,

Just to continue this thread for a moment...

Suppose I'm writing a class for a statistical distribution. I write the
appropriate constructors, properties and methods (in this case a simple
mean, or average). In this case I would just use the declaration 'public
double', and its usage would be:

StatDist test = new StatDist();
double Avg = test.Average();

However, if I want to also write a general purpose Average function in
the same class, I'd want to overload the function using 'public static
double' so that its usage would be:

double x[] = { ... };
double Avg = StatDist.Average(x);

Is it a good (or bad) idea to overload the function and make one of them
static all in the same class?

Regards,
Schiz
 
S

Schizoid Man

Jon said:
Right. In that case, not only could all the methods be static, but you
might want to mark the whole *class* as being static. That would
prevent it being instantiated in the first place.

Hi Jon, Bob,

Just to continue this thread for a moment...

Suppose I'm writing a class for a statistical distribution. I write the
appropriate constructors, properties and methods (in this case a simple
mean, or average). In this case I would just use the declaration 'public
double', and its usage would be:

StatDist test = new StatDist();
double Avg = test.Average();

However, if I want to also write a general purpose Average function in
the same class, I'd want to overload the function using 'public static
double' so that its usage would be:

double x[] = { ... };
double Avg = StatDist.Average(x);

Is it a good (or bad) idea to overload the function and make one of them
static all in the same class?

Regards,
Schiz
 
J

Jon Skeet [C# MVP]

Schizoid Man said:
Just to continue this thread for a moment...

Suppose I'm writing a class for a statistical distribution. I write the
appropriate constructors, properties and methods (in this case a simple
mean, or average). In this case I would just use the declaration 'public
double', and its usage would be:

StatDist test = new StatDist();
double Avg = test.Average();

However, if I want to also write a general purpose Average function in
the same class, I'd want to overload the function using 'public static
double' so that its usage would be:

double x[] = { ... };
double Avg = StatDist.Average(x);

Is it a good (or bad) idea to overload the function and make one of them
static all in the same class?

I think it's reasonable in some cases. If you have a look at
Regex.Replace, it has exactly that sort of thing - various overloads,
some of which are static method and take a pattern, and some of which
are instance methods which use the target regular expression for the
pattern.
 
S

Schizoid Man

Jon said:
Schizoid Man said:
Just to continue this thread for a moment...

Suppose I'm writing a class for a statistical distribution. I write the
appropriate constructors, properties and methods (in this case a simple
mean, or average). In this case I would just use the declaration 'public
double', and its usage would be:

StatDist test = new StatDist();
double Avg = test.Average();

However, if I want to also write a general purpose Average function in
the same class, I'd want to overload the function using 'public static
double' so that its usage would be:

double x[] = { ... };
double Avg = StatDist.Average(x);

Is it a good (or bad) idea to overload the function and make one of them
static all in the same class?

I think it's reasonable in some cases. If you have a look at
Regex.Replace, it has exactly that sort of thing - various overloads,
some of which are static method and take a pattern, and some of which
are instance methods which use the target regular expression for the
pattern.

Thank you.
 
B

Ben Voigt [C++ MVP]

Thank you all for the replies - I really appreciate it. I have a C++
background and am preparing for an interview.

Suppose I am writing a general utility 'class' (simple functions like Max,
Min, Average, etc) that does not need access to instance members because
each function is self contained.

Wouldn't it be inefficient for me to instantiate the Utility class and
then class the appropriate Utility.Method rather than applying the method
straight away without the creating the instance?

I could achieve this in C++ by declaring the methods outside of a class in
the header and then include the appropriate header in the source file.

What would be the equivalent in C#? Interpreting Bob's description,
declaring the appropriate methods as static would be the way to go.

Thanks again.

C++ uses static in *exactly* the same way. From the Visual C++ Language
Reference:

"When modifying a data member in a class declaration, the static keyword
specifies that one copy of the member is shared by all instances of the
class. When modifying a member function in a class declaration, the static
keyword specifies that the function accesses only static members."

Of course this is highly inaccurate. Static member functions can access
non-static members, both functions and data, as long as an instance of the
class is referenced in the process. Since the static member function is a
member of the class, it can access private and protected members in this way
as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top