Is static function a right coding practice in this case, performance-wise and resource usage-wise?
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?
Here's a sample code I've to implement, which is just showing up forms:
private void btn1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.ShowDialog();
}
Now I've to show this Form1
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 Form1
to be displayed, or should it be better to define it once as a st开发者_运维百科atic method in a static class, like this:
public static class globalvars
{
public static void Form1Show()
{
Form1 frm = new Form1();
frm.ShowDialog();
}
}
And then:
private void btn1_Click(object sender, EventArgs e)
{
globalvars.Form1Show();
}
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?
I know both works, and doesn't make a big difference, but still, which is the right coding practice, memory usage wise?
From a pure coding perspective any solution that avoid duplicating code is good. In your case you might want to add a static method to your Form1:
public Form1 : Form
{
public static void CreateNew()
{
using (var form = new Form1())
{
form.ShowDialog();
}
}
[...]
}
EDIT: From a memory point of view, the two samples you provided behave exactly the same. And modal dialogs should be disposed explicitly (with the using keyword for example).
Maybe I lost something there, but I didn't see any difference between them. The frm
is a local variable, not a static field.
From a memory management standpoint since you're showing the same form in multiple places the optimal approach would be to implement the singleton pattern to ensure only one instance is used
All - I know this isn't exactly relevant as the question has been answered, but in order to answer nawfal's question regarding the singleton pattern I'm going to post a brief description and some code here.
@Nawfal - the singleton pattern is used to ensure that only a single instance of an object is ever used in an application. There are a huge number of different ways to achieve this, check out MSDN singleton pattern introduction for a basic introduction.
For non-trivial objects, e.g. classes this is the code I like to use:
1: Create a base Singleton class
public abstract class SingletonBase<T> where T : class
{
protected SingletonBase() { }
public static T Instance
{
get { return SingletonFactory.Instance; }
}
/// <summary>
/// The singleton class factory to create the singleton instance.
/// </summary>
class SingletonFactory
{
static SingletonFactory() { }
SingletonFactory() { }
internal static readonly T Instance = GetInstance();
static T GetInstance()
{
var theType = typeof(T);
T inst;
try
{
inst = (T)theType
.InvokeMember(theType.Name,
BindingFlags.CreateInstance | BindingFlags.Instance
| BindingFlags.NonPublic,
null, null, null,
CultureInfo.InvariantCulture);
}
catch (MissingMethodException ex)
{
throw new TypeLoadException(string.Format(
CultureInfo.CurrentCulture,
"The type '{0}' must have a private constructor to " +
"be used in the Singleton pattern.", theType.FullName)
, ex);
}
return inst;
}
}
2: Now inherit from your base class
public class myClass : SingletonBase<myClass>
{
// this is a private constructor
myClass() { }
private void somemethod()
{
}
public void SomeOtherMethod()
{
}
}
3: Finally anywhere you need to use myClass you call its instance as shown below
class Program
{
static void Main(string[] args)
{
// here you call the single instance of myClass
myClass myClassInstance = myClass.Instance;
// run the public someothermethod of myClass
myClassInstance.SomeOtherMethod();
}
精彩评论