public class Student
    {
        public string name;
        public int ID;
    }
    public class telephoneList
    {
        public void fuzhi()
        {
            Student[] Students = new Student[3];
            Students[0].name = "张三";
            Students[0].ID = 10000;
            Students[1].name = "李四";
            Students[1].ID = 11000;
            Students[2].name = "王五";
            Students[2].ID = 12000;
        }
        public int this[string thename]
        {
            get
            {
                Student[] Students = new Student[3];                for (int i = 0; i < 4; i++)
                {
                    if(Students[i].name ==(thename))
                    return Students[i].ID;
                }
                return 0;
            }
        }
    }
    public class output
    {
        static void Main()
        {
            telephoneList thetelephoneList = new telephoneList();
            Console.WriteLine(thetelephoneList["张三"]);
        }
    }请问,我这个代码错在哪里,?

解决方案 »

  1.   

    public class Student
        {
            public string name{get;set;}
            public int ID { get; set; }
        }
        public class telephoneList
        {  
            Student[] Students = new Student[3];
            public telephoneList()
            {
                for (int i = 0; i < 3; i++)
                {
                    Students[i] = new Student();//实例化
                }
                Students[0].name = "张三";
                Students[0].ID = 10000;
                Students[1].name = "李四";
                Students[1].ID = 11000;
                Students[2].name = "王五";
                Students[2].ID = 12000;
            }
            public int this[string thename]
            {
                get
                {
                    for (int i = 0; i < 4; i++)
                    {
                        if (Students[i].name == (thename))
                            return Students[i].ID;
                    }
                    return 0;
                }
            }
        }    class Program
        {
         
            static void Main(string[] args)
            {            telephoneList thetelephoneList = new telephoneList();
                Console.WriteLine(thetelephoneList["张三"]);            Console.ReadKey();
            }
         
        }
     10000
      

  2.   

     public telephoneList()
      {
      for (int i = 0; i < 3; i++)
      {
      Students[i] = new Student();//实例化
      }
    请问,这一部分是不是不带参数的构造函数?不带参数的构造函数不是系统自己整的吗?如果是,在这里写个构造函数是啥意思呢,谢谢?
      

  3.   

    嗯,我知道实例化,我就是说咋写成 public telephoneList()
    呢,不写其它样式呢?这不是构造函数吗
      

  4.   

    public telephoneList()构造函数中实例化数组赋值
    也可通过对象初始化器赋值
      

  5.   

    另外,我想问一下
    class IndexClass
    {
    private string[] name=new string[10]; 
      public string this[int index]    
    {
         get { return name[index]; }      
     set { name[index]=value; }
       }
    }
    class test
    {
        static void Main()
          {
     
      IndexClass b=new IndexClass();
      b[0]="张三"; 
      b[1]="李四";  
      b[2]="王五";
      Console.WriteLine("b[0]="+b[0]);
      Console.WriteLine("b[1]="+b[1]);
      Console.WriteLine("b[2]="+b[2]);
       Console.ReadKey();
    }
    }这段代码,创建数组对象后,直接就赋值了,并没有一个一个new,为什么你帮我写的那个代码,要用个for循环来全部new一下呢?谢谢
      

  6.   

    上面自定义Students类的数组实例不同于string这种数组实例,Students[] demoStudent = new Students[10]这种只是在栈上分配了空间并引用到堆上的对象,所以你要使用这种数组初始化赋值之前还另外需要new一下进行实例化。而int, string这种,如int[] demoNumber = new int[10]就不需要像自定义类数组实例化时候那样干~
      

  7.   

    修改下:上面自定义Students类的数组实例化不同于string这种数组实例化,Students[] demoStudent = new Students[10]这种只是在栈上分配了空间但并未引用到堆上的对象,所以你要使用这种数组初始化赋值之前还另外需要new一下进行实例化。而int, string这种,如int[] demoNumber = new int[10]就不需要像自定义类数组实例化时候那样干~
      

  8.   

    不是这个函数的问题,是你自己长度没有取对for循环里面应该是3不是4
      

  9.   

    因为每个对象使用之前你都要先用构造函数啊....否则都没给内存中分配地址空间怎么用啊...不知道你理解的一般函数是什么意思...是public void telephoneList()这种...吗...
      

  10.   

    哪位老师,麻烦教授一下,为什么这里必须要用构造函数呢,该成一般函数为啥不行啊,用了那么多new对象,从来没有说是必须要用构造函数啊
      

  11.   

    这个你有点钻牛角尖了,对象你要在赋值使用之前必须被实例化的也就是说必须在内存空间上也块地址的,只不过不同对象实例化的时候可能会不同就像这里int, string数组这种就只需要new一次就行了,而自定义的类要new两次一样,而具体为什么一定要得用构造函数来分配内存空间...这就是规定嘛...硬要追求原因那你只有问微软了...你现在说为什么一定要用构造函数而不用一般的那种函数,就像在问人为什么一定要呼吸空气啊...不能呼吸氢气一个性质了...