Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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中输入字典代码_C#_Class_Dictionary - Fatal编程技术网

C# 无法在C中输入字典代码

C# 无法在C中输入字典代码,c#,class,dictionary,C#,Class,Dictionary,似乎无论我在代码或类中的何处输入Dictionary,它都不会被接受,并且会产生一个错误。我得到了括号和圆括号的无效标记,我得到了System.Collections.Generic您提出问题的方式似乎缺少类型参数。尖括号里的东西 第一种是键类型。 第二种是值类型。 。首先确保包含名称空间 using System.Collections.Generic; 你可以像这样申报一本字典 var dictionary = new Dictionary<string, int>(); //

似乎无论我在代码或类中的何处输入Dictionary,它都不会被接受,并且会产生一个错误。我得到了括号和圆括号的无效标记,我得到了System.Collections.Generic您提出问题的方式似乎缺少类型参数。尖括号里的东西

第一种是键类型。 第二种是值类型。 。首先确保包含名称空间

using System.Collections.Generic;
你可以像这样申报一本字典

var dictionary = new Dictionary<string, int>(); // this is the best way!
dictionary.Add("4", 4);
dictionary.Add("230", 230);
字典上的一些常见操作

dictionary.Add("42", 42);      
dictionary.Count();            // 3
dictionary.ContainsKey("3");   // false
dictionary.ContainsValue(230); // true
dictionary.Remove("230");
dictionary.Count();            // 2
dictionary.Clear();
dictionary.Count();            // 0

你能给我们看一下你的代码,让我们看看你是如何实际声明字典的吗?请添加一个完整的代码示例。@StephenWilson我不需要提供任何示例代码,因为我只需要知道如何声明字典,而一个简单的谷歌搜索并没有给出我想要的结果。@MythrilMagician-你真的需要好好地与SO社区合作。因此,请在提问时发布您的代码,帮助So社区将您的问题转化为优质资源。非常感谢您的回答。但还有一个问题:如何将每个新的listbox项添加到字典中?到目前为止,我已经完成了这个公共无效字典{var Dictionary=newdictionary;}
dictionary.Add("42", 42);      
dictionary.Count();            // 3
dictionary.ContainsKey("3");   // false
dictionary.ContainsValue(230); // true
dictionary.Remove("230");
dictionary.Count();            // 2
dictionary.Clear();
dictionary.Count();            // 0