Tuesday, February 14, 2012

Create your first code template for MonoDevelop

I switched from VisualStudio to MonoDevelop as my primary IDE not long time ago. Wat satisfies me in MonoDevelop most it is the level of customization you can applay to it... I was long time user of VisulaStudio and I always used VisualStudio defaults. Monoevelop triggered something in me and in few weeks I created MonokaiStyle for MonoDevelop and MonoDevelop custom code templates. Now I'll show you how you can create yours code templates... Tools->Options
Text Editor->Code Templates->C# and then press add. We will implement IDisposable code template as an example. Start by pasting the following code into code editor:
namespace MyNamespace
{
 public class MyClass : IDisposable
 {
  private bool _disposed = false;

  public MyClass()
  {
  }

  public bool IsDisposed
  {
   get { return _disposed; }
   private set { _disposed = value; }
  }

  protected virtual void Dispose(bool disposing)
  {
   if (!IsDisposed)
   {
    if (disposing)
    {
    }

    IsDisposed = true;
   }
  }

  public void Dispose()
  {
   Dispose(true);
   GC.SuppressFinalize(this);
  }

  ~MyClass()
  {
   Dispose(false);
  }
 }
}
Now we will replace namespace and class names with placeholders (placeholders starts and ends with '$' characters). Your template should look like this:
namespace $MyNamespace$
{
 public class $MyClass$ : IDisposable
 {
  private bool _disposed = false;

  public $MyClass$()
  {
  }

  public bool IsDisposed
  {
   get { return _disposed; }
   private set { _disposed = value; }
  }

  protected virtual void Dispose(bool disposing)
  {
   if (!IsDisposed)
   {
    if (disposing)
    {
    }

    IsDisposed = true;
   }
  }

  public void Dispose()
  {
   Dispose(true);
   GC.SuppressFinalize(this);
  }

  ~$MyClass$()
  {
   Dispose(false);
  }
 }
}
press OK (now your template will be saved).
Now you can use it Edit->Insert template->dsp