本文介绍了集合被修改枚举数被实例化后 - Request.ServerVariables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要避免错误枚举数被实例化后集合被修改
我发现了一些建议,通过收集Request.ServerVariables.Keys循环之前,使用以下行:

  IEnumerator的EN = Request.ServerVariables.Keys.GetEnumerator();
en.MoveNext();

纵观,他们设置了服务器变量的集合,然后通过该集合迭代,而不调用的GetEnumerator和MoveNext的。

两个问题:


  1. 为什么我们需要调用的GetEnumerator和MoveNext的?

  2. 这是如果使用ASP.NET和C#4.0的更好的办法?


解决方案

您也可以使用:

 的String []键= Request.ServerVariables.AllKeys;
 的foreach(在钥匙串键)
 {
      如果(Request.ServerVariables [关键]!= NULL)
      {
           字符串值= Request.ServerVariables [关键]
      }
 }

To avoid the error "Collection was modified after the enumerator was instantiated" I found a number of recommendations to use the following lines before looping through the Request.ServerVariables.Keys collection:

IEnumerator en = Request.ServerVariables.Keys.GetEnumerator();
en.MoveNext();

Looking at the MSDN example, they set the Server Variables to a collection, then iterate through that collection without calling GetEnumerator or MoveNext.

Two questions:

  1. Why do we need to call GetEnumerator and MoveNext?
  2. Which is the better approach if using ASP.NET and C# 4.0?
解决方案

You can also use:

 string[] keys = Request.ServerVariables.AllKeys;
 foreach(string key in keys )
 {
      if(Request.ServerVariables[key] != null)
      {
           string value = Request.ServerVariables[key];
      }
 }

这篇关于集合被修改枚举数被实例化后 - Request.ServerVariables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 08:23