为什么这些代码必须放在Main方法里,放在别的方法里调用会产生错误?
IDictionary environment = Environment.GetEnvironmentVariables();
Console.WriteLine("环境变量名\t=\t环境变量值");
foreach (string environmentKey in environment.Keys)
{
Console.WriteLine("{0}\t=\t{1}", environmentKey, environment[environmentKey].ToString());
}
IDictionary environment = Environment.GetEnvironmentVariables();
Console.WriteLine("环境变量名\t=\t环境变量值");
foreach (string environmentKey in environment.Keys)
{
Console.WriteLine("{0}\t=\t{1}", environmentKey, environment[environmentKey].ToString());
}
using System.Collections.Generic;
using System.Text;{
class Seven
{ public void show()
{
IDictionary environment = Environment.GetEnvironmentVariables();
Console.WriteLine("环境变量名\t=\t环境变量值");
foreach (string environmentKey in environment.Keys)
{
Console.WriteLine("{0}\t=\t{1}", environmentKey, environment[environmentKey].ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Test
{
class Start
{
public static void Main(String[] args)
{
Seven seven = new Seven();
seven.CreateControlRegion();
}
}
}
错误 1 使用泛型 类型“System.Collections.Generic.IDictionary<TKey,TValue>”需要“2”个类型参数 D:\My Documents\Visual Studio 2005\Projects\Test\Test\Seven.cs 11 13 Test
错误 2 无法将带 [] 的索引应用于“IDictionary”类型的表达式 D:\My Documents\Visual Studio 2005\Projects\Test\Test\Seven.cs 15 66 Test
这样做就会报哪两个错误
把main所在文件的命名空间全copy过去试试
public void show() 啥意思???
foreach (DictionaryEntry de in environmentVariables)
{
Console.WriteLine(" {0} = {1}", de.Key, de.Value);
}
参考
http://www.cnblogs.com/jun1111/archive/2008/08/21/1273398.html
坐在一个类里,用启动类生成对象然后调用方法会出错误。。using System;
using System.Collections.Generic;
using System.Text;{
class Seven
{ public void show()
{
IDictionary environment = Environment.GetEnvironmentVariables();
Console.WriteLine("环境变量名\t=\t环境变量值");
foreach (string environmentKey in environment.Keys)
{
Console.WriteLine("{0}\t=\t{1}", environmentKey, environment[environmentKey].ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Test
{
class Start
{
public static void Main(String[] args)
{
Seven seven = new Seven();
seven.show();
}
}
}
错误 1 使用泛型 类型“System.Collections.Generic.IDictionary <TKey,TValue>”需要“2”个类型参数 D:\My Documents\Visual Studio 2005\Projects\Test\Test\Seven.cs 11 13 Test
错误 2 无法将带 [] 的索引应用于“IDictionary”类型的表达式 D:\My Documents\Visual Studio 2005\Projects\Test\Test\Seven.cs 15 66 Test
这样做就会报哪两个错误
而你添加的using System.Collections.Generic; 这个里面也有个IDictionary,不过这个IDictionary是IDictionary <TKey,TValue>
是你命名空间搞错了