title: Concurrent acces to a static member in .NET
body: <p>I've a class that contains a static collection to store the logged-in users in an ASP.NET MVC application. I just want to know about the below code is thread-safe or not. Do I need to lock the code whenever I add or remove item to the onlineUsers collection.</p>
  
  <pre><code>public class OnlineUsers
  {
      private static List&lt;string&gt; onlineUsers = new List&lt;string&gt;();
      public static EventHandler&lt;string&gt; OnUserAdded;
      public static EventHandler&lt;string&gt; OnUserRemoved;
  
      private OnlineUsers()
      {
      }
  
      static OnlineUsers()
      {
      }
  
      public static int NoOfOnlineUsers
      {
          get
          {
              return onlineUsers.Count;
          }
      }
  
      public static List&lt;string&gt; GetUsers()
      {
          return onlineUsers;
      }
  
      public static void AddUser(string userName)
      {
          if (!onlineUsers.Contains(userName))
          {
              onlineUsers.Add(userName);
  
              if (OnUserAdded != null)
                  OnUserAdded(null, userName);
          }
      }
  
      public static void RemoveUser(string userName)
      {
          if (onlineUsers.Contains(userName))
          {
              onlineUsers.Remove(userName);
  
              if (OnUserRemoved != null)
                  OnUserRemoved(null, userName);
          }
      }
  }
  </code></pre>