tags:

views:

128

answers:

5

I use resharper and resharper adviced me to declare one method as static and the another not. But i can't understand why the other method can't be static ?

method recommended to be static

 private static string Prehod(string retazec)
    {
        var pole = retazec.ToCharArray();
        var output = "";
        char? temp = null;

        for (var i = 0; i < pole.Length; i++)
        {
            if (temp == null)
            {
                temp = pole[i];
                continue;
            }

            output += pole[i].ToString() + temp.ToString();
            temp = null;
        }
        return output;
    }

and method not recommended to be static

 public string HashToString(string hash,int dlzka)
    {
        var hashChar = hash.Substring(0, dlzka*2);
        var retazec = "";

        for (var i = 0; i < dlzka*2; i++)
        {
            if(i%2 != 0)
            {
                retazec += hashChar.Substring(i, 1);
            }
        }
        return retazec;
    }
A: 

I can't see any reason why the second method can't be static, as to why Resharper suggests one and not the other... you'd have to ask the Resharper developers. Remember, it's a tool, not a rule book.

Lazarus
+2  A: 

Resharper doesn't give advices on public members of your classes, since they can be used by other classes.

But it's still a sign (not to say 'smell') for you if public instance methods don't need instance at all.

elder_george
A: 

I don't see why your second method couldn't be static. As far as I can tell, it doesn't access any instance fields.

It wouldn't be a problem mnaking it static. So much to your question why it couldn't be static. As for ReSharper, I don't know why it doesn't recommend it as static whereas it does this for the other method.

Maximilian Mayerl
A: 

Your first method is private, and probably used statically inside the class. Eg: var ehy = Prehod("testing");.

The second method is public. Probably you use it already somewhere like:

   var okBaby = new MyClass();
   Console.WriteLine(okBaby.HashToString("something", 10));

And resharper probably thinks there is a reason for it to be so, and doesn't suggest the change.

Just a blind shot, though.

Alex Bagnolini
A: 

Both methods can be static: they don't use any instance members of your class. And both methods can be instance methods. But a method being static or not should be implied by the possible uses of it, not by ReSharper. Do you expect your method to be called as part of an instance:

YourClass instance = new YourClass();
string x = instance.Prehod(...);

or do you expect your method to be called as part of the class:

string x = YourClass.Prehod(...);

It can be much later in the development process before you realize which is best. If you want to give users of your class the "feel" that it acts on the instance (new ClassName()), you must choose instance method. If you really need the method to be used without an instance (ClassName.YourMethod()) you must choose static.

UPDATE: it is uncommon to make a private method static, unless you need other static methods to call the private static methods. If only other instance methods use your private method, then there's no reason to make it a static method whatsoever.

Abel
Resharper suggests making private methods static because it is a (very slight) performance increase - the 'this' parameter isn't used so it doesn't need to be given to the method.
configurator
The performance increase is much debated and is hard or even impossible to proof in practice. Semantics, use and clarity should drive the choice, not the `callvirt` vs. `call` difference. More here, which also discusses the related FxCop rule: http://gregbeech.com/blogs/tech/archive/2007/01/11/static-vs-instance-method-performance.aspx
Abel