0

我有类同义词,如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;


namespace Examples.NET
{
    public class Synonym
    {
        private string _kata;
        private List<string> _sinonim;

        public String Kata
        {
            get { return _kata; }
        }
        public List<string> Sinonim 
        {
            get 
            {
                return _sinonim; 
            }
        }

        public Synonym(string kata)
        {
            _kata = kata;
            List<string> _sinonim = new List<string>();

            XDocument xDoc = XDocument.Load("http://www.stands4.com/services/v2/syno.php?uid=2319&tokenid=LSRyvL5mGsHpEi4&word=" + kata);
            var sinonim = from isi in xDoc.Descendants("result")
                          select new
                          {
                              sinom = isi.Descendants("synonyms").First().Value
                          };
            foreach (var s in sinonim)
            {
                _sinonim.Add(s.sinom);
            }
        }

    }
}

在主程序中,我有以下代码:

Synonym Syn = new Synonym("buy");

System.Console.WriteLine(Syn.Kata);

var sinom = from s in Syn.Sinonim
            select s;
foreach (var item in sinom)
{
  System.Console.WriteLine("data: " + item);
}

但是,出现错误:在主程序中:“FROM S IN .....SELECT S”===========>>值不能为空。AND 参数名称:source

我想课堂上有错误

  public List<string> Sinonim 
  {
   get{ return _sinonim;}
  }
4

1 回答 1

1

这表明您的私有列表变量从未被分配,但事实并非如此。

List<string> _sinonim = new List<string>();

这隐藏了私有实例,因为名称相同。将构造函数中的那一行更改为此,它应该可以工作

_sinonim = new List<string>();

于 2012-11-17T10:46:56.513 回答