English 中文(简体)
C# multi-key dictionary and TryGetValue
原标题:C# multi-key dictionary and TryGetValue

试图用2个钥匙做一个词典,因为它似乎比制造2个具有同样价值的字典好。 我想得到两个钥匙中一个的价值,即如果两个钥匙中有1个符合标准,它就会收回价值。 我还认为,我可以增加2个具有不同关键因素的内容,但对于一个字典来说,同样价值。

我是这样说的,但我想知道,这样做是否更加有效。 或者如果在不使用多基字典的情况下获得我所希望的东西还有另一种更好的途径

    public bool TryGetValue(out TValue value, TKey1 key1 = default, TKey2 key2 = default)
    {
        value = default;

        if (EqualityComparer<TKey1>.Default.Equals(key1, default) && EqualityComparer<TKey2>.Default.Equals(key2, default))
            return false;

        foreach (var pair in this)
        {
            if ((EqualityComparer<TKey1>.Default.Equals(key1, default) || pair.Key.Item1.Equals(key1))
                && (EqualityComparer<TKey2>.Default.Equals(key2, default) || pair.Key.Item2.Equals(key2)))
            {
                value = pair.Value;
                return true;
            }
        }

        return false;
    }
最佳回答
问题回答

任务是将具有不同类型钥匙的物体列入字典,但标准字典由同类钥匙界定。

你们可能有不同的钥匙界定自己的物体,这些钥匙需要放在一个字典上,因此钥匙可以不同类型。

最简单的方式是,对关键目标进行接口,然后这些物体可以相同(通用接口类型)。

守则实例显示了两个具有共同接口的关键结构,以及该接口的字典工程(伊凯)。

值得记住的是,词典要求(至少)实施一个关键的“平等”和“发展”;接口,以及关键功能的实施——字典的业绩取决于这一点。

public interface IKey : System.IEquatable<IKey>
{
}
public struct Key1 : IKey
{
    public int Value;
    public Key1(int value)
    {
        Value = value;
    }
    public override int GetHashCode()
    {
        // !! return adequate hashcode
        return Value.GetHashCode();
    }
    public bool Equals(IKey other)
    {
        // !! return adequate equals
        return Value.Equals(((Key1)other).Value);
    }
    public override string ToString()
    {
        return Value.ToString();
    }
}
public struct Key2 : IKey
{
    public string Value;
    public Key2(string value)
    {
        Value = value;
    }
    public override int GetHashCode()
    {
        // !! return adequate hashcode
        return Value.GetHashCode();
    }
    public bool Equals(IKey other)
    {
        // !! return adequate equals
        return Value.Equals(((Key2)other).Value,
                            System.StringComparison.Ordinal);
    }
    public override string ToString()
    {
        return Value;
    }
}
public class DictionaryIKey<TValue> :
    System.Collections.Generic.Dictionary<IKey, TValue>
{
    public void Add(int key, TValue value)
    {
        Add(new Key1(key), value);
    }
    public void Add(string key, TValue value)
    {
        Add(new Key2(key), value);
    }

    public bool TryGetValue(int key, out TValue value)
    {
        return TryGetValue(new Key1(key), out value);
    }
    public bool TryGetValue(string key, out TValue value)
    {
        return TryGetValue(new Key2(key), out value);
    }
}

public static void Run()
{
    DictionaryIKey<string> dict = new DictionaryIKey<string>(3);

    string value = "123456, John, Duus";

    // add object to dictionary, and this object is accessible
    // by keys of different types

    dict.Add(123456, value);
    dict.Add("John", value);
    dict.Add("Duus", value);

    string _out;
    dict.TryGetValue(123456, out _out);
    System.Console.WriteLine(_out);
    dict.TryGetValue("John", out _out);
    System.Console.WriteLine(_out);
    dict.TryGetValue("Duus", out _out);
    System.Console.WriteLine(_out);

    dict.TryGetValue(111000, out _out);
    System.Console.WriteLine(_out);
    dict.TryGetValue("no??", out _out);
    System.Console.WriteLine(_out);

    // output:
    // 123456, John, Duus
    // 123456, John, Duus
    // 123456, John, Duus
    // <empty>
    // <empty>
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签