Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Static methods differ from instance methods in that <em>no instance</em> of the class they belong to needs to have been created for them to be called. When you call a static method, you in fact make the call using the <em>name of the type</em> rather than an instance of the type - which should reinforce the idea that static methods are not called on instances. <em>That bears repeating and emphasis</em>: <strong>No instance of a class is required to call a public static method of that class.</strong></p> <p><strike>Now, your example is malformed, but presumably,</strike> the line: <code>if( Foo.SomeCheck() )</code> is calling the <code>SomeCheck</code> static method using the name of the type: <code>Foo</code> - not an instance. <strong>Bar however, has to be instantiated in order to make this call</strong> - <strike>however, in your example, you don't have a well-formed instance of <code>Bar</code>. Code generally has to exist inside a method (or a member initializer) - which you don't have here.</strike></p> <p>To respond to the other parts of your question. Assuming the code in question is part of an instance method, something has to instantiate <code>Bar</code> - and invoke that method. That <em>something</em> would have to create or otherwise acquire an instance of <code>Bar</code>. Reference types will always be creted on the heap - but that's largely irrelevant here.</p> <p>As for garbage collection, you normally shouldn't worry about this. The .NET runtime makes sure to cleanup instances that are not referenced from any root object in your program. Roots are typically instances that reside somewhere on the callstack or are referenced by static members of one type or another. Since we don't see any code here that creates or references <code>Bar</code> it's impossible to say <em>when</em> it will be collected. For instance, if <code>Bar</code> is a singleton and stored somewhere in a static variable, it may live for a very long time - perhaps the entire lifetime of the program. You can't really know without seeing <em>all</em> of the code that manipulates and manages <code>Bar</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload