问题1:
对于在循环里定义变量是不是不科学,请看下边的代码foreach(XmlNode newsItemNode in newsItemNodes)
{
//变量的定义放在循环外和循环里,对内存的分配是不是也有很大影响?
//比如下行代码:我定义一个字符串变量 
string str = newsLinkNode.InnerText;//对此变量的定义放在循环里会不会浪费内存
request = (HttpWebRequest)HttpWebRequest.Create(newsLinkNode.InnerText);
request.Timeout= 10000;
response= (HttpWebResponse)request.GetResponse();
stream= response.GetResponseStream();
reader= new StreamReader(stream,Encoding.GetEncoding("GB2312"),false,bufferSize);
//注意上行代码:在这个循环里每进行一次,都需要在堆上创建reader对象的一个实例,这样做是不是也是对内存的一种浪费,因为new会在堆上开辟一块儿内存,保存一个对象的实例,所以,如果在循环里new的话,每次都会开辟一块儿内存,如果是这样的话,就用StreamReader reader来说吧,为什么StreamReader类的定义者不给该类的对象提供一个可供修改的Stream属性,这样,在循环里,我只需要每次指派reader对象的Stream属性即可,就不会每次都在内存里开辟新的内存区域,这样岂不是很节省内存?不过我不知道我说的是不是对的,高手予以指教
readPosition= reader.Read(buffer,0,buffer.Length);
newsContentBuilder.Remove(0,newsContentBuilder.Length);while(readPosition > 0)
{
//...对流的处理
}
}*********************************************************************************问题2:
DirectoryEntry DirEntry = new DirectoryEntry("IIS://LocalHost/W3SVC");// loop through all the properties and get the key for each
//notice:here will throw System.NotSupportedException.
foreach (string Key in DirEntry.Properties.PropertyNames) //此行代码抛出异常(System.NotSupportedException 目录无法报告属性的数量
{
string PropertyValues = String.Empty;// now loop through all the values in the property;
// can be a multi-value property
foreach (object Value in DirEntry.Properties[Key])
PropertyValues += Convert.ToString(Value) + ";";// cut off the separator at the end of the value list
PropertyValues = PropertyValues.Substring(0, PropertyValues.Length - 1);// now add the property info to the property list
Collection.Add(Key + "=" + PropertyValues);
}