看到书上的一个练习题,找出这段程序的问题,如果有问题就修改,没问题就写出结果
using System;
using System.Collections.Generic;
using System.Text;
enum Color 
{
    red, yellow, blue, green, purple, black, white
};namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Color[] color_arr = new Color[3];
            int[] int_arr = new int[] { 0, 1, 2 };
            color_arr = (Color[])int_arr;  //运行的时候这里报错
            Console.WriteLine("The value of color[0] is:{0}",color_arr[0]);
            Console.WriteLine("The value of color[1] is:{0}", color_arr[1]);
            Console.WriteLine("The value of color[2] is:{0}", color_arr[2]);
        }
    }
}虽然已经找到了错误,但是这么解决呢?不懂!
只好请教各位了!谢谢
顺便问一个非常简单的问题,如何能让命令行不会一运行完就自动关闭啊?

解决方案 »

  1.   

    for (int i = 0; i < int_arr.Length; i++)
    {
       color_arr[0] = (Color)int_arr[i];
    }
      

  2.   

    应该是这个吧?
    for (int i = 0; i < int_arr.Length; i++)
    {
    color_arr[i] = (Color)int_arr[i];
    }
    但是再问一个问题,为什么我用上面这位哥们儿给的代码,运行结果会是
    The value of color[0] is:blue
    The value of color[1] is:red
    The value of color[2] is:red
    高手请解释一下!谢谢
      

  3.   

    不带[]的数组,实际上是该数组的起始地址,或者说句柄,如果楼主学过VC或者C++,更容易明白些,你要转换的是里面的内容,而不是地址,而且地址也不可以这么转换