Note that there are some explanatory texts on larger screens.

plurals
  1. POIs static function a right coding practice in this case, performance-wise and resource usage-wise?
    primarykey
    data
    text
    <p>There are many many similar links in stackoverflow, all which seem to be addressing complex functions (so quite difficult for me to understand what i really should go for). But mine is simple. I just want to know if declaring a function as static right way to go, if i can manage the same functionality even with a nonstatic approach? Which approach utilizes more memory?</p> <p>Here's a sample code I've to implement, which is just showing up forms:</p> <pre><code> private void btn1_Click(object sender, EventArgs e) { Form1 frm = new Form1(); frm.ShowDialog(); } </code></pre> <p>Now I've to show this <code>Form1</code> from many other forms in my program. So should it be better (right coding practice) to write the above piece of code where ever I want <code>Form1</code> to be displayed, or should it be better to define it once as a static method in a static class, like this:</p> <pre><code>public static class globalvars { public static void Form1Show() { Form1 frm = new Form1(); frm.ShowDialog(); } } </code></pre> <p>And then:</p> <pre><code> private void btn1_Click(object sender, EventArgs e) { globalvars.Form1Show(); } </code></pre> <p>I think if object of form is instantiated under the event where we want the form to be displayed (first non static approach), then the memory allocated for that process in the stack is destroyed right after its execution; while in the second static approach, the memory allocated stands throughout the lifetime of the application, and hence non-static approach is better right?</p> <p>I know both works, and doesn't make a big difference, but still, which is the right coding practice, memory usage wise?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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