代码1:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace DVD_Shop
{
    /// <summary>
    /// 加载CD信息.
    /// </summary>
    public class CD
    {
        public string name;   //cd名称
        public double price;     //cd价格
        public string type;   //cd类型        public CD(string n, double p, string t)
        {
            name = n;
            price = p;
            type = t;
        }
    }    /// <summary>
    /// VideoShop DVD商店类
    /// </summary>
    public class VideoShop
    {
        private CD[] CDCollection = new CD[100];  //商店CD柜台
        private int CDCount = 0;                           //商店CD数目
        public int Count { get { return CDCount; } }       //返回当前CD数目        public CD this[string CDName]
        {
            get
            {
                foreach( CD t in CDCollection )
                    if(t.name == CDName)
                        return t;
                return null;
            }
        }        public CD this[string CDName, string type]
        {
            get
            {
                foreach (CD t in CDCollection)
                    if(t.name == CDName && t.type == type)
                        return t;
                return null;
            }
        }        public CD this[int n]
        {
            set
            {
                if (CDCollection[n] == null)
                    CDCount++;
                if (value == null && CDCollection[n] != null)
                    CDCount--;
                CDCollection[n] = value;
            }            get
            {
                return CDCollection[n];
            }
        }
    }
  
       class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            VideoShop MyShop = new VideoShop();
            
            CD cd1 = new CD("误入歧途", 10.5, "DVD");
            CD cd2 = new CD("罗马假日", 5.5, "VCD");
            
            MyShop[MyShop.Count] = cd1;
            MyShop[MyShop.Count] = cd2;            CD a = MyShop["罗马假日"];
            CD b = MyShop["误入歧途","DCD"];            Console.WriteLine("名称:{0}{1},价格:{2}", a.type, a.name, a.price);
            Console.WriteLine("名称:{0}{1},价格:{2}", b.type, b.name, b.price);
            Console.Read();
        }
    }
}
using System;
namespace TestSpace
{
    public class CD
    {
        public string name; //CD名称
        public double price;//CD价格
        public string type; //CD类型
        public CD(string n, double p, string t)
        { name = n; price = p; type = t; }
    }
    public class VideoShop
    {
        private CD[] cdCollection = new CD[10000]; //商店CD集合
        private int cdCount = 0; //CD数目
        public int Count { get { return cdCount; } } //返回当前CD数目
        public CD this[string cdName]
        {
            get
            {
                //按CD名称返回CD对象
                foreach (CD t in cdCollection)
                    if (t.name == cdName) return t;
                return null;
            }
        }
        public CD this[string cdName, string type]
        {
            get
            {
                //按CD名称和类型返回CD对象
                foreach (CD t in cdCollection)
                    if (t.name == cdName && t.type == type) return t;
                return null;
            }
        }
        public CD this[int n]
        {
            set
            {
                //如果是商店里没有此CD则cdCount增加
                if (cdCollection[n] == null) cdCount++;
                if (value == null && cdCollection[n] != null) cdCount--;
                cdCollection[n] = value;
            }
            get { return cdCollection[n]; }
        }
    }
    class Test
    {
        [STAThread]
        static void Main(string[] args)
        {
            VideoShop myshop = new VideoShop();
            CD cd1 = new CD("误入歧途", 10.5, "DVD");
            CD cd2 = new CD("罗马假日", 5.5, "VCD");
            myshop[myshop.Count] = cd1; //调用索引器赋值
            myshop[myshop.Count] = cd2; //调用索引器赋值
            CD a = myshop["罗马假日"]; //调用索引器按名称获得CD
            CD b = myshop["误入歧途", "DVD"]; //调用索引器按类型和名称获得CD
            Console.WriteLine("名称:{0}{1},价格:{2}", a.type, a.name, a.price);
            Console.WriteLine("名称:{0}{1},价格:{2}", b.type, b.name, b.price);
        }
    }
}
我检查了3遍,觉得没什么地方不一样,可是执行的结果就是不一样,特别奇怪,这是一个索引器的问题.
C#和.NET第一步, 书上一个范例.. 我检查不出来,高手们帮我找找错误 .谢谢了.

解决方案 »

  1.   

        错误是if(t.name == CDName && t.type == type)
        t 的值是null, 
        这个集合哦,怎么变成了null.!
        异常, 未处理的NullReferenceException!
      

  2.   

    写错了:   CD b = MyShop["误入歧途","DCD"];    foreach (CD t in CDCollection)
           if(t.name == CDName && t.type == type)
              return t;foreach 遍历CDCollection数组中的所有元素,即使元素值为null当DVD写正确时,foreach找到结果就返回了,写错成不存在的类型时,foreach会访问到有效元素之后的null元素