-2

What is the benefits of declaring a private method as static and when to use it?

Why should I use:

    private static string GetSomething()
    {
        return "Something";
    }

instead of:

    private string GetSomething()
    {
        return "Something";
    }
Stacked
  • 6,892
  • 7
  • 57
  • 73
  • 1
    First result from google: http://msdn.microsoft.com/en-us/library/98f28cdx.aspx Second result: http://msdn.microsoft.com/en-us/library/79b3xss3.aspx Third result: http://stackoverflow.com/questions/4124102/whats-a-static-method-in-c# – dtb Sep 06 '13 at 09:45
  • How about providing some context? Both examples are valid, it depends on the scenario. – DGibbs Sep 06 '13 at 09:48
  • 1
    Accessors and the static-modifier are orthogonal concepts, so there is no relation between `private` and `static`. – Tim Schmelter Sep 06 '13 at 09:49
  • @dtb The three results do not answer my question! I know what static means, but why should I declare a PRIVATE method as STATIC? my class is not static, what are the posibble cases to use a private static method in my class? – Stacked Sep 06 '13 at 09:55
  • In C++, declaring a function static, means a 'this' pointer is not also passed to the function, therefore it's faster (for varying values of faster). Perhaps C# is similar in this respect? – Neil Sep 06 '13 at 10:00
  • 1
    @Stacked - You know what static means, but did you understand it? Static basically means on class level. So every method that works exactly the same for every instance of a class and does not need anything instance specific, like the value of a property or something, could be declared as static. - For your example, the method just returns a string. It's the same string for every instance and you don't use anything instance specific, so it could be declared static to show the clear intend, that it is not bound to an instance. – Corak Sep 06 '13 at 10:00
  • @Corak "...returns the same string for every instance" this is more to the point. Thanks – Stacked Sep 06 '13 at 10:03
  • Can the downvoters tell me the reason for the -3 votes? – Stacked Sep 06 '13 at 10:12

3 Answers3

3

This is what MSDN say

A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction, which also checks for a null object references. However, most of the time the performance difference between the two is not significant.

Which in simpler word means that there will some performance gain.

Ehsan
  • 31,833
  • 6
  • 56
  • 65
3

You might declare a private static method as a means of signalling to other developers that this method does not use instance data.

Jaycee
  • 3,098
  • 22
  • 31
0

Consider a below simple design, where private static has significance.

You want to have some data hiding (abstraction) and want to use that data in both static and non-static functions of a class:

Consider below example:

    public class test
    {
        private static int funcValue()
        {
            return 200;
        }
        public static void someStaticFunc()
        {
            int x = funcValue();
        }
        public void anotherNonStaticFunc()
        {
            int y = funcValue();
        }
    }

calling:

test t = new test();
test.someStaticFunc();//static
t.anotherNonStaticFunc();//non-static

In the above example, if you do not declare funcValue as static, you can not call it in the static function someStaticFunc and also you want funcValue to be private (abstraction).

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34