C# - Static and Private

Author
Discussion

him_over_there

Original Poster:

970 posts

207 months

Wednesday 16th June 2010
quotequote all
Can someone tell me what the point of a method declared as both private and static within a class ?

I am doing some code refactoring and the tool wants me to declare a currently private method as static but I don't want to change it to public.

Thanks,

(I'm not making it static as there is no need for it to be static, just curious as to what logic would require a method that is both private and static..) smile

Edited by him_over_there on Wednesday 16th June 10:04

ChrisMCoupe

927 posts

213 months

Wednesday 16th June 2010
quotequote all
I'm a Java guy but can only assume it is for a method which will only be called within the class itself, and the method wants/needs to belong to the Class rather than an instance of the Class.


julian64

14,317 posts

255 months

Wednesday 16th June 2010
quotequote all
I assume it means that the same data will be held for every instance of the class.

zippy3x

1,316 posts

268 months

Wednesday 16th June 2010
quotequote all
Any public static method in a class will only be able to access static class variables or other static methods.

A private static method will therefore be like any other private method in a class, but accessable by other static methods.

Blown2CV

29,061 posts

204 months

Thursday 17th June 2010
quotequote all
I am too a Java guy, but its basically the same deal. Static members can be accessed by instances, and private static members can be accessed by any instance of that class so there is a use for them. Perhaps the purpose of this might be called into question, but it is legitimate.

dilbert

7,741 posts

232 months

Thursday 17th June 2010
quotequote all
Isn't this usually when there is some fixed resource at application scope?

The object provides a private static method by which it manages that resource, and then a user can create as many instances (perhaps in different thread contexts) as he/she wants. The class manages all the different instances that want access to that shared resource.

If the method was public, it would obviate the genuinely useful code in the object, and all of the users would be fighting over the single resource it represents.

I think this comes under the general description of "Marshaling, Serialisation and Synchronisation".

I'm afraid to say that I'm C++.

Edited by dilbert on Thursday 17th June 16:57

130R

6,814 posts

207 months

Thursday 17th June 2010
quotequote all
Here's an example - Imagine you have 2 static methods A and B that do similar things. Some of the code in A and B may be the same so you might want to refactor that code into a separate method in the same class and have A and B call that method (duplication of code = bad). You should always use the most restrictive visibility applicable, so you declare a private static method.

Edited by 130R on Thursday 17th June 19:08

dilbert

7,741 posts

232 months

Thursday 17th June 2010
quotequote all
130R said:
Here's an example - Imagine you have 2 static methods A and B that do similar things. Some of the code in A and B may be the same so you might want to refactor that code into a separate method in the same class and have A and B call that method (duplication of code = bad). You should always use the most restrictive visibility applicable, so you declare a private static method.

Edited by 130R on Thursday 17th June 19:08
It might be different for C#, but in C++, it wouldn't matter if it was static or not (except that by making it static one would have to pass a "this pointer" to it).

In C++ one of the key benefits of a static member function is that you can use it as a reference when you call a function that requires a _cdecl callback. Non-static member functions in C++ have the _thiscall calling convention and because of their decoration, the linker can (quite rightly) not manage them properly.

Typically the only time one might need that capability is when one is using a a shared system resource.

I'm pretty sure that the static keyword in C# and Java has other meanings too.

Edited by dilbert on Thursday 17th June 19:38

130R

6,814 posts

207 months

Thursday 17th June 2010
quotequote all
dilbert said:
It might be different for C#, but in C++, it wouldn't matter if it was static or not (except that by making it static one would have to pass a "this pointer" to it).
In Java and C# static methods cannot call non-static methods without an instance of the class (since a non-static method is associated with an instance). Also in Java and C# the this keyword refers to the current instance of the class, which you do not have available in a static method since a static method is not associated with an instance but the class itself. You could create a new instance in a static method or pass an instance into the method of course.

dilbert

7,741 posts

232 months

Thursday 17th June 2010
quotequote all
130R said:
dilbert said:
It might be different for C#, but in C++, it wouldn't matter if it was static or not (except that by making it static one would have to pass a "this pointer" to it).
In Java and C# static methods cannot call non-static methods without an instance of the class (since a non-static method is associated with an instance). Also in Java and C# the this keyword refers to the current instance of the class, which you do not have available in a static method since a static method is not associated with an instance but the class itself. You could create a new instance in a static method or pass an instance into the method of course.
Obviously my emphasis is on passing a this pointer to the static member, how one does that is more or less inconsequential. Usually such an argument is passed as a void pointer, with an explicit cast....

It sounds more or less the same then.

I find that a bit weird. Whenever I've seen example Java code, the static keyword is peppered around everywhere. For me, I know the kinds of things I can do with it, and rarely find it useful. When it is useful, nothing else would do. Perhaps that's because Java and C# don't allow global functions, not associated with a class. Certainly, I'd code those as I would have in C. At least that way there's no doubt, at least not to me. At the very least you don't have to keep typing "static".

smile

GnuBee

1,272 posts

216 months

Friday 18th June 2010
quotequote all
Private Static in a class - I can see a use...

You have a static class it does a bunch of stuff and has a public static method called DoYourThing(). Part of the implementation requires some functionality in another member call DoVeryDangerousThingWithParameter(string Thing)

Now you'd not want the DoVeryDange... method to just be called by anyone so you define it as Private Static. So in other words it's a way of putting the intricacies of some of your implementation in a static class and making sure it remains hidden from consumers or inheritors.