1

In this code, is there a difference between the static function and the non-static one?

And, beside the fact that a static function belongs to the object and not to the instance, are there more differences?

public class ClassName
{
   public void f1(SomeObject n)
   {
      n.someProperty = 1;
   }
   public static void f1(ref SomeObject n)
   {
      n.someProperty = 1;
   }
}

duplicate? Yes, so it seems. just note that in the other question there were no answers that addressed the question itself.

yossi
  • 3,090
  • 7
  • 45
  • 65
  • 1
    No, there is no other differences hence for non-static method call you need to create `ClassName` instance. –  Feb 05 '15 at 08:06
  • 1
    This code won't even compile. You can't overload a method solely on instance/static. – rossipedia Feb 05 '15 at 08:08
  • 2
    @rossipedia I don't think the example code was meant to compile, but yes, you are right – Jcl Feb 05 '15 at 08:08
  • changed it, tho @Jcl is correct. – yossi Feb 05 '15 at 08:12
  • 1
    result is the same but best practice is to use a static call. there are 3 advantages: 1. memory footprint reduction as mentioned by JCL, 2. More intuivite since there is no ambiguity about setting up the state first by calling other methods, 3. Much easier to Unit Test (basically because of reason 2). – shelbypereira Feb 05 '15 at 08:13
  • #1 is incorrect. The instance method doesn't affect runtime size at all. – rossipedia Feb 05 '15 at 08:14
  • 1
    SomeObject is passed by ref. So, if it is holding reference to shared resource then use locks before modifying it. – Amit Feb 05 '15 at 08:20

3 Answers3

5

Except those that you already mention: no, there's no difference. In fact, functions that don't really affect (or require to know the status of) an instance of the same object, should be preferred static (that would, at the very least, reduce the memory footprint of every instance of the object).

Update

Now that I think of it, I don't think it'd really affect the memory footprint. So go with what looks better, "design-wise"

Just adding, that if a method (as mentioned) doesn't need the instance of an object, "design-wise", it should be better as static. If anything, because you don't have to instantiate an object to use it.

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • You are correct. Methods/Functions don't add to runtime size of an instance at all. – rossipedia Feb 05 '15 at 08:13
  • 2
    @rossipedia that's right, it took me a couple minutes to think about it. Fields would, but not methods. – Jcl Feb 05 '15 at 08:14
-1

Why have f1 associated with ClassName. You should extend SomeObject.

public static class SomeObjectExtensions
{
   public static void f1(this SomeObject o) { o.SomeProperty = 1; }
}

Then your code can do:

var so = new SomeObject();
so.f1();
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
-1

Judging only by the lines you've posted in the question you should definitely use the static version because calling an instance method has it's performance penalties:

1.) Calling an instance method behind involves loading the instance on the evaluation which is the "this" argument, which is avoided calling a static method.

2.) Calling an instance method compiles to the "Callvirt" IL opcode that performs a null check for the "this" first passed argument and then tries to resolve the calling runtime actual instance method, this applies to all instance methods, not only for the virtual ones as the name might wrongly imply. Calling a static methods on the other hand will compile to the "call" IL opcode which will just call the method.

3.) Static methods are stateless by their nature which makes it easier for multithreaded design ( in case they do use an external shared static field).

Roman Ambinder
  • 369
  • 3
  • 7
  • 1
    "1.) The IL for an instance method is generated for each instance created, and static method's IL is generated only once." This is **absolutely** incorrect. – rossipedia Feb 05 '15 at 08:29
  • Thanks for the comment, i'll look in to that more thoroughly. Currently i removed it from the answer. – Roman Ambinder Feb 05 '15 at 08:36