title: Relationship between HttpContext.Request.Cookies and HttpContext.Response.Cookies
body: <p>I have been experimenting with code that will clear all of the cookies in an <code>HttpContext.Response</code>.</p>
  
  <p>Initially, I used this:</p>
  
  <pre><code>DateTime cookieExpires = DateTime.Now.AddDays(-1);
  
  for (int i = 0; i &lt; HttpContext.Request.Cookies.Count; i++)
  {
      HttpContext.Response.Cookies.Add(
          new HttpCookie(HttpContext.Request.Cookies[i].Name, null) { Expires = cookieExpires });
  }
  </code></pre>
  
  <p>However, this will error with an <code>OutOfMemoryException</code> because the <code>for</code> loop never exits - each time you add a cookie to the <code>Response</code>, it also gets added to the `Request.</p>
  
  <p>The following approach works:</p>
  
  <pre><code>DateTime cookieExpires = DateTime.Now.AddDays(-1);
  
  List&lt;string&gt; cookieNames = new List&lt;string&gt;();
  
  for (int i = 0; i &lt; HttpContext.Request.Cookies.Count; i++)
  {
      cookieNames.Add(HttpContext.Request.Cookies[i].Name);
  }
  
  foreach (string cookieName in cookieNames)
  {
      HttpContext.Response.Cookies.Add(
         new HttpCookie(cookieName, null) { Expires = cookieExpires });
  }
  </code></pre>
  
  <p>So, what exactly is the relationship between <code>HttpContext.Request.Cookies</code> and <code>HttpContext.Response.Cookies</code>?</p>