Monthly Archives: January 2014

Code Snippet examples

By   January 23, 2014
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>log4net</Title>
      <Author>Jia KANG</Author>
      <Description>Code snippet for an log4net logger</Description>
      <Shortcut>log4net</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>class_name</ID>
          <Function>ClassName()</Function>
        </Literal>
        <Literal Editable="false">
          <ID>ilog</ID>
          <Function>SimpleTypeName(global::log4net.ILog)</Function>
        </Literal>
        <Literal Editable="false">
          <ID>log_man</ID>
          <Function>SimpleTypeName(global::log4net.LogManager)</Function>
        </Literal>
        <Literal Editable="true">
          <ID>_log</ID>
          <Type />
          <ToolTip />
          <Default>_log</Default>
          <Function />
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[private static readonly $ilog$ $_log$ = $log_man$  .GetLogger(typeof($class_name$).Name);]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>propgo</Title>
      <Author>Jia KANG</Author>
      <Description>Code snippet for get-only property and backing private readonly field</Description>
      <HelpUrl></HelpUrl>
      <SnippetTypes />
      <Keywords />
      <Shortcut>propgo</Shortcut>
    </Header>
    <Snippet>
      <References />
      <Imports />
      <Declarations>
        <Literal Editable="true">
          <ID>_myAttribute</ID>
          <Type></Type>
          <ToolTip></ToolTip>
          <Default>_myAttribute</Default>
          <Function></Function>
        </Literal>
        <Literal Editable="true">
          <ID>int</ID>
          <Type></Type>
          <ToolTip></ToolTip>
          <Default>int</Default>
          <Function></Function>
        </Literal>
        <Literal Editable="true">
          <ID>MyAttribute</ID>
          <Type></Type>
          <ToolTip></ToolTip>
          <Default>MyAttribute</Default>
          <Function></Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="method decl" Delimiter="$"><![CDATA[private readonly $int$ $_myAttribute$;

public $int$ $MyAttribute$
{
    get { return $_myAttribute$; }
}]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

IDisposable implementation example

By   January 23, 2014

    public class MyClass : IDisposable
    {
        private readonly Timer _timer;

        public MyClass()
        {
            _timer = new Timer(Timer_OnElapsed, null, 0, 1000);
        }

        private void Timer_OnElapsed(object sender)
        {
        }

        #region IDisposable members

        private bool _disposed = false;

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

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _timer.Dispose();
                }
                _disposed = true;
            }
        }

        ~MyClass()
        {
            Dispose(false);
        }

        #endregion IDisposable members
    }