标签: idictionary

从IEnumerable <KeyValuePair <>>重新创建字典

我有一个返回的方法IEnumerable<KeyValuePair<string, ArrayList>>,但有些调用者要求方法的结果是字典.如何将其IEnumerable<KeyValuePair<string, ArrayList>>转换为Dictionary<string, ArrayList>可以使用的TryGetValue

方法:

public IEnumerable<KeyValuePair<string, ArrayList>> GetComponents()
{
  // ...
  yield return new KeyValuePair<string, ArrayList>(t.Name, controlInformation);
}
Run Code Online (Sandbox Code Playgroud)

呼叫者:

Dictionary<string, ArrayList> actual = target.GetComponents();
actual.ContainsKey("something");
Run Code Online (Sandbox Code Playgroud)

c# collections ienumerable dictionary idictionary

165
推荐指数
2
解决办法
9万
查看次数

将对象映射到字典,反之亦然

是否有任何优雅的快速方法将对象映射到字典,反之亦然?

例:

IDictionary<string,object> a = new Dictionary<string,object>();
a["Id"]=1;
a["Name"]="Ahmad";
// .....
Run Code Online (Sandbox Code Playgroud)

SomeClass b = new SomeClass();
b.Id=1;
b.Name="Ahmad";
// ..........
Run Code Online (Sandbox Code Playgroud)

.net c# mapping dictionary idictionary

79
推荐指数
7
解决办法
9万
查看次数

将KeyValuePair列表转换为IDictionary"C#"

我的情景,

怎么转换List<KeyValuePair<string, string>>IDictionary<string, string>

c# idictionary

39
推荐指数
2
解决办法
2万
查看次数

为什么C#Dictionary没有实现所有的IDictionary?

我想创建一个类似于字典的对象,并认为正确的方法是实现IDictionary<K,V>接口,并使用组合来包含底层字典.我从下面开始(K= string,V= int)

public class DictionaryLikeObject : IDictionary<string,int> {
  Dictionary<string,int> _backingDictionary = new Dictionary<string,int>();
}
Run Code Online (Sandbox Code Playgroud)

然后我使用Visual Studio的"实现接口"功能来删除我需要的所有封面方法.

IDictionary似乎不存在三种方法Dictionary:

void Add(KeyValuePair<string, int> item);
void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex);
bool Remove(KeyValuePair<string, int> item);
Run Code Online (Sandbox Code Playgroud)

然而,Microsoft文档清楚地表明了这些Dictionary工具IDictionary.所以我希望这三种方法可用.要从文档中复制,请定义Dictionary<K,V>

[SerializableAttribute]
[ComVisibleAttribute(false)]
public class Dictionary<K, V> : IDictionary<K, V>, 
ICollection<KeyValuePair<K, V>>, IEnumerable<KeyValuePair<K, V>>, 
IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback
Run Code Online (Sandbox Code Playgroud)

我相信这三种缺失的方法都可以在ICollection<>.而且还有其他方法,比如Clear()那个Dictionary确实有.

问题1: 如果没有实现这三个,C#如何逃脱?为什么会这样?我怀疑这是一个编译错误(我的推理,见下文). 问题2: 或者,我错过了什么? …

c# dictionary idictionary

26
推荐指数
1
解决办法
5928
查看次数

在xaml中创建一个字典?

伪示例:

<Window>
  <Window.Tag>
    <x:Dictionary KeyType="{x:Type sys:String}" ValueType="{x:Type sys:Int32}">
        <sys:DictionaryEntry Entry="{sys:DictionaryEntry Key0, 000}"/>
        <sys:DictionaryEntry Key="key1" Value="111"/>
        <sys:DictionaryEntry>
          <sys:DictionaryEntry.Key>
            <sys:String>Key2<sys:String>
          </sys:DictionaryEntry.Key>          
          <sys:DictionaryEntry.Value>
            <sys:Int32>222</sys:Int32>            
          </sys:DictionaryEntry.Value>
        </sys:DictionaryEntry>
    </x:Dictionary />    
  </Window.Tag>
</Window>
Run Code Online (Sandbox Code Playgroud)

xaml dictionary idictionary key-value

21
推荐指数
4
解决办法
2万
查看次数

Dictionary.ContainsKey() - 它是如何工作的?

我已经阅读了关于如何Dictionary.ContainsKey()工作的MSDN文档,但我想知道它实际上是如何进行相等比较的?基本上,我有一个键入引用类型*的字典,我希望该ContainsKey()方法检查该引用类型的某个属性作为确定密钥是否存在的基础.举例来说,如果我有一个Dictionary(MyObject, int)MyObject拥有(的公共财产int被称为"TYPEID"),我能得到ContainsKey(MyObject myObject)检查,看是否的关键之一具有TypeID等于myObject?我可以超载==操作员吗?

  • 引用类型是一个名为"Duration"的对象,它包含一个value(double Length); "持续时间"是我的音乐节目中使用的基本类型,表示特定声音持续多长时间.我从中派生出类,其中包含更复杂的时序概念,如西方音乐时间签名,但希望所有这些都在长度方面具有可比性.

编辑:正如所建议的,我在我的对象上实现了IEquitable,如下所示:

 public class Duration : IEquatable<Duration>
 {
    protected double _length;

    /// <summary>
    /// Gets or Sets the duration in Miliseconds.
    /// </summary>
    public virtual double Length
{
        get
        {
            return _length;
        }
        set
        {
            _length = value;
        }
    }

// removed all the other code that as it was irrelevant

    public override bool Equals(object …
Run Code Online (Sandbox Code Playgroud)

.net c# idictionary reference-type

21
推荐指数
2
解决办法
2万
查看次数

为什么Dictionary.ContainsKey抛出ArgumentNullException?

文档声明bool Dictionary<TKey, TValue>.ContainsKey(TKey key)在传递null键时抛出异常.谁能说出理由呢?如果它刚刚归来,它会不会更实际false

.net c# exception idictionary

14
推荐指数
2
解决办法
9134
查看次数

C# - 需要一个允许空键的IDictionary实现

基本上,我想要这样的东西:

Dictionary<object, string> dict = new Dictionary<object, string>();
dict.Add(null, "Nothing");
dict.Add(1, "One");
Run Code Online (Sandbox Code Playgroud)

是否有任何内置到基类库中允许这样做?添加null键时,前面的代码将在运行时抛出异常.

谢谢

c# collections idictionary

13
推荐指数
2
解决办法
9840
查看次数

如何初始化ConcurrentDictionary?错误:"无法访问私有方法'在此处添加'

我有一个静态类,我在其中使用字典作为查找表来映射.NET类型和SQL类型.这是一个这样的字典的例子:

private static readonly Dictionary<Type, string> SqlServerMap = new Dictionary<Type, string>
{
    {typeof (Boolean), "bit"},
    {typeof (Byte[]), "varbinary(max)"},
    {typeof (Double), "float"},
    {typeof (Byte), "tinyint"},
    {typeof (Int16), "smallint"},
    {typeof (Int32), "int"},
    {typeof (Int64), "bigint"},
    {typeof (Decimal), "decimal"},
    {typeof (Single), "real"},
    {typeof (DateTime), "datetime2(7)"},
    {typeof (TimeSpan), "time"},
    {typeof (String), "nvarchar(MAX)"},
    {typeof (Guid), "uniqueidentifier"}
};
Run Code Online (Sandbox Code Playgroud)

然后我在下面有一个公共方法,它传入一个.NET类型,它使用这个字典返回相应的MS SQL Server类型的字符串值.但是,由于这被用作进行数据库查询的查找表,我认为将其作为一个有意义ConcurrentDictionary.我改成了:

private static readonly IDictionary<Type, string> SqlServerMap = new ConcurrentDictionary<Type, string>
{
    {typeof (Boolean), "bit"},
    {typeof (Byte[]), "varbinary(max)"},
    {typeof (Double), "float"},
    {typeof (Byte), "tinyint"}, …
Run Code Online (Sandbox Code Playgroud)

c# dictionary private idictionary concurrentdictionary

13
推荐指数
3
解决办法
5328
查看次数

字典<>中的条目是否有限制?

我需要组织大约3000个不同的文件,并在游戏过程中的不同时间检索.

我创建了自己的变量结构.我正在考虑在我的应用程序开始时创建一个"Dictionary",并在游戏开始之前简单地加载我的所有文件.

我想知道性能:有这么多条目的字典会导致我的应用程序变慢吗?大字典是否会使"TryGetValue"和"ContainsKey"运行得更慢?

感谢您的建议!

c# silverlight performance dictionary idictionary

9
推荐指数
3
解决办法
2万
查看次数