解决方案 »

  1.   

    想发现bug,不要靠“细心”,应该靠亲自写测试。如果不厌其烦地要求别人“细心读代码”和“必须有洁癖”的,容易走火入魔,热衷于巨大争吵内耗,浪费公家的散碎银子。
      

  2.   

    你这个代码,在第一个foreach循环后边应该写上            Debug.Assert( myDictStore.Any(x=>x.Value .Contains(  "1"));
    我从来不追求好高骛远标准“没谱”的关于“细心”的说法,我则是会思考这两个并不过分要求的Debug.Assert断言是否能够写出来?!
      

  3.   

    Debug.Assert(myDictStore.Any(x => x.Value.Contains("5")));
    Debug.Assert(myDictStore.Any(x => x.Value.Contains("1")));
      

  4.   


    List<string> myTmpList = new List<string>();
    放循环内申请。           
               foreach (var item in myStrList)
                {
                    List<string> myTmpList = new List<string>();                string[] myTmpString = item.Split(new char[] { '|' });
                    myTmpList.Clear();
                    myTmpList.AddRange(myTmpString);
                    myDictStore.Add(item, myTmpList);                 myTmpList = null;
                }
      

  5.   

    List<string> myStrList = new List<string> { "1|2|3","4|5|6"};
    List<string> myTmpList =  myStrList.SelectMany(x => x.Split('|')).ToList();
      

  6.   

    myDictStore.Add(item, myTmpList);
    这里面存的是数组
    所以打印的时候要做2层循环遍历数组
    否则打出来的是数组类型名称
      

  7.   


    除了跑很完善的单元测试,加Debug.Assert也是一个很好的办法。赞!
      

  8.   


    谢谢回复。你的这2句代码的确可以实现我的功能。
    我举此例主要是想说明循环外定义List<string> myTmpList 引起的bug。
    变量复用所引起的bug
      

  9.   

    代码越多bug越多。我的原则是,一个类的代码不要超过显示器一页。
      

  10.   


    自己查找bug时,除了单元测试和Debug,Assert ,还有其他办法没得?
      

  11.   


    除了这些原则性问题,你们是通过社么办法让自己代码里的bug降到最少的?
      

  12.   

    所有Dictionary的value都指向的是一个List对象如果List对象中的内容是基础类型的话可以这样
    myDictStore.Add(item, myTmpList);
    改为
    myDictStore.Add(item, myTmpList.ToList());
      

  13.   

    List<string> myTmpList = new List<string>();  请问各位大大, 
    我可否任为这里的泛型声明,就相当于申请了一个字符串数组的   myTmpList  指针 ?myTmpList  也只是代表了一个地址?
    当程序执行  myTmpList.Clear();  时,也就是说,myTmpList 的内容被清空, 同时,指针回到初始时状态。
    myDictStore.Add(item, myTmpList); 时, 这里ADD的并不是一个数组的值,而只是数组的地址?所以在循环中,我们其实并没有将之前的值保存下来,而是保存了 myTmpList  的地址。而13#的写法   myDictStore.Add(item, myTmpList.ToList());  是不是可以说是将 myTmpList  指向的值写到了 Dictionary 内?
      

  14.   


    除了这些原则性问题,你们是通过社么办法让自己代码里的bug降到最少的?写客户端程序不觉得,但是如果写服务器端程序,一定要单元测试。否则调试就是一场灾难。
      

  15.   

    TDD只是不太好在队伍里面大面积推,很容易受到抵制。但如果落实下来,项目及文档的健壮性会很有保障!
      

  16.   


    你理解的好像有偏差,myTmpList.ToList()返回了一个和myTmpList所指向的list对象一样的list对象而已(但是其实不是一个对象)所以dictionary里的多个value指向的是多个对象,所以任你myTmpList指向的对象怎么改变也不会改变value指向的对象。