Volatile Keyword in C#

I came across this keyword, when our code needed a lock in case of thread safe Singleton class.

So here was the code:

public class ModuleCatalogue
{
   private static volatile ModuleCatalogue _moduleCatalogue;
   private static object syncRoot = new object();
   private ModuleCatalogue
   {}
   public static ModuleCatalogue instance
   {
    get
   {
     if (_moduleCatalogue == null)
     {
       lock (syncRoot)
      {

      if (_moduleCatalogue == null)
      _moduleCatalogue = new ModuleCatalogue()
      }
     }
    return _moduleCatalogue;
   }
  }
}

So why do we need to make _moduleCatalogue volatile. This is mainly because our compiler tries to be smart and tries to do some optimization by caching the value. So we use the keyword “volatile” to make sure it does not do any optimization on that memory location.And thus making sure that the thread retrieves the most up to date value. It is also said that the compiler optimizes some section of the code as well. This might affect our value in case of muti-threaded environment.