You can use Lazy<T>
.
public partial class Form1 : Form
{
Lazy<MyClass> obj = new Lazy<MyClass>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
obj.Value.StartWork();
}
private void button2_Click(object sender, EventArgs e)
{
obj.Value.StopWork();
}
}
Accessing its Value
property will force the creation of object.
You can also supply a custom initialization function to Lazy
constructor.