Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#中初始化字典返回溢出错误_C#_Dictionary - Fatal编程技术网

在c#中初始化字典返回溢出错误

在c#中初始化字典返回溢出错误,c#,dictionary,C#,Dictionary,我创建了一个字典,如您所见: public Dictionary<string, string> TipList { get { return TipList; } set { TipList = value; } } 公共词典TipList { 获取{return TipList;} 设置{TipList=value;} } 我从服务中获取了一些数据,我想将这些数据放入我的字典中,您可以在这里看到: Dictionary<string, string>

我创建了一个字典,如您所见:

public Dictionary<string, string> TipList
{
    get { return TipList; }
    set { TipList = value; }
}
公共词典TipList
{
获取{return TipList;}
设置{TipList=value;}
}
我从服务中获取了一些数据,我想将这些数据放入我的字典中,您可以在这里看到:

Dictionary<string, string> dict = new Dictionary<string, string>();
try
{
    using (var response = (HttpWebResponse)request.GetResponse())
    {

        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            var objText = reader.ReadToEnd();
            var list = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(objText).ToDictionary(x => x.Keys, x => x.Values);
            object o;
            object o1;
            foreach (var item in list)
            {
                o = item.Value.ElementAt(0);
                o1 = item.Value.ElementAt(1);
                dict.Add(o.ToString(), o1.ToString());
            }
            GlobalVariable.TipListCache.Add(NewCarReceiption.CSystem.Value, dict);
            NewCarReceiption.TipList = dict.Where(i=>i.Key!=null & i.Value!=null).ToDictionary(x => x.Key, x => x.Value);
        }
    }
}
Dictionary dict=new Dictionary();
尝试
{
使用(var response=(HttpWebResponse)request.GetResponse())
{
使用(var reader=newstreamreader(response.GetResponseStream())
{
var objText=reader.ReadToEnd();
var list=JsonConvert.DeserializeObject(objText.ToDictionary)(x=>x.Keys,x=>x.Values);
对象o;
对象o1;
foreach(列表中的变量项)
{
o=项目值元素(0);
o1=项目价值元素(1);
dict.Add(o.ToString(),o1.ToString());
}
GlobalVariable.TipListCache.Add(NewCarReceiption.CSystem.Value,dict);
NewCarReceiption.TipList=dict.Where(i=>i.Key!=null&i.Value!=null)。ToDictionary(x=>x.Key,x=>x.Value);
}
}
}
但在运行我的代码后,当上述函数试图将其数据放入我的字典时。我的应用程序返回此错误:


您正在一次又一次地设置相同的属性,进入一个无限循环

如果在getter和setter中不需要任何额外的逻辑,可以让它自动实现:

public Dictionary<string, string> TipList
{
   get;
   set;
}
公共词典TipList
{
得到;
设置
}
如果您确实需要getter和setter中的更多逻辑,您必须自己添加一个支持字段:

private Dictionary<string, string> tipList;
public Dictionary<string, string> TipList
{
    get
    {
        DoSomethingBeforeGet();
        return this.tipList;
    }
    set
    {
        DoSomethingBeforeSet();
        this.tipList = value;
        DoSomethingAfterSet();
    }
}
私有字典tipList;
公共词典提普利斯特
{
得到
{
DoSomethingBeforeGet();
把这个还给我;
}
设置
{
DoSomethingBeforeSet();
this.tipList=值;
DoSomethingAfterSet();
}
}

您反复设置相同的属性,进入无限循环

如果在getter和setter中不需要任何额外的逻辑,可以让它自动实现:

public Dictionary<string, string> TipList
{
   get;
   set;
}
公共词典TipList
{
得到;
设置
}
如果您确实需要getter和setter中的更多逻辑,您必须自己添加一个支持字段:

private Dictionary<string, string> tipList;
public Dictionary<string, string> TipList
{
    get
    {
        DoSomethingBeforeGet();
        return this.tipList;
    }
    set
    {
        DoSomethingBeforeSet();
        this.tipList = value;
        DoSomethingAfterSet();
    }
}
私有字典tipList;
公共词典提普利斯特
{
得到
{
DoSomethingBeforeGet();
把这个还给我;
}
设置
{
DoSomethingBeforeSet();
this.tipList=值;
DoSomethingAfterSet();
}
}

您的setter正在调用
TipList
属性的setter(自身),该属性正在调用其setter,依此类推-导致异常

按如下方式初始化它:

private Dictionary<string, string> _tipList;
public Dictionary<string, string> TipList
{
    get { return _tipList; }
    set { _tipList = value; }
}
private Dictionary\u tipList;
公共词典提普利斯特
{
获取{return\u tipList;}
设置{u tipList=value;}
}
或者,如果您不需要默认行为以外的任何行为,则最好使用:

公共字典TipList{get;set;}
由于C#6.0,您也可以这样初始化它(使用自动属性初始值设定项):

公共字典TipList{get;set;}=new Dictionary();

您的setter正在调用
TipList
属性的setter(自身),该属性正在调用其setter,依此类推-导致异常

按如下方式初始化它:

private Dictionary<string, string> _tipList;
public Dictionary<string, string> TipList
{
    get { return _tipList; }
    set { _tipList = value; }
}
private Dictionary\u tipList;
公共词典提普利斯特
{
获取{return\u tipList;}
设置{u tipList=value;}
}
或者,如果您不需要默认行为以外的任何行为,则最好使用:

公共字典TipList{get;set;}
由于C#6.0,您也可以这样初始化它(使用自动属性初始值设定项):

公共字典TipList{get;set;}=new Dictionary();