简体   繁体   English

在 c# 中实现字典

[英]Implement Dictionary in c#

I was asked in an interview to implement the dictionary.How to implement it?I have tried implementing it using index as key using array.But Was unable to implement generic dictionary.我在一次采访中被问到实现字典。如何实现它?我尝试使用索引作为键使用数组来实现它。但是无法实现通用字典。

There are many ways to implement a class like Dictionary<T1, T2> .有很多方法可以实现 class ,例如Dictionary<T1, T2>
I will describe a simple one.我将描述一个简单的。

  1. Create a class and a List that stores all contents.创建一个 class 和一个存储所有内容的 List。
    [Edit] We will compare variables T1 values so a restriction, where T1: IEquatable<T1> is required. [编辑] 我们将比较变量 T1 的值,以便限制, where T1: IEquatable<T1>是必需的。
class MyDictionary<T1, T2> where T1 : IEquatable<T1>
{
    private List<(T1 key, T2 val)> m_internal_data;
}
  1. Implement a function that find a value inside the class.实现一个 function,它在 class 中找到一个值。
    [Edit] Use Equals function. [编辑] 使用Equals function。 Using == causes an error.使用==会导致错误。
public T2 Find(T1 key)
{
    // Looking for a content.
    foreach (var content in m_internal_data)
    {
        if (content.key.Equals(key))
        {
            return content.val;
        }
    }
    // It reaches here when there is no content which has the same key.
    // Then, I recommend to throw an exception or return a default value of T2.
    return default(T2);
}
  1. Implement a function that assign a value.实现一个分配值的 function。
    [Edit] Use Equals too. [编辑] 也使用Equals
public void Store(T1 key, T2 val)
{
    // Looking for a content. If exists, store a new value.
    for (int i = 0; i < m_internal_data.Count; i++)
    {
        if (m_internal_data[i].key.Equals(key))
        {
            var content = m_internal_data[i];
            content.val = val;
            m_internal_data[i] = content;
            return;
        }
    }
    // Create a new key.
    m_internal_data.Add((key, val));
}
  1. Make it able to access a value by using square brackets.使其能够使用方括号访问值。 Just call previous functions.只需调用以前的函数。
public T2 this[T1 key]
{
    get => Find(key);
    set
    {
        Store(key, value);
    }
}

That's it.而已。

Of course, this is not highly-optimized and has few useful functions.当然,这不是高度优化的,并且几乎没有有用的功能。 If you want to know how to write Dictionary more useful, I recommend you to read GitHub dotnet/runtime Dictionary.cs , which is included in .NET Core.如果您想知道如何编写更有用的 Dictionary,我建议您阅读GitHub dotnet/runtime Dictionary.cs ,它包含在 .NET Core 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中的字典上实现linq? - How to Implement linq on dictionary in C#? C# - 添加实现接口到字典的对象 - C# - Adding objects that implement interfaces to a dictionary C#应用程序如何为此实现字典或哈希表? - C# application how could i implement dictionary or hashtable for this? 我如何在C#中实现QueueDictionary,Queue和Dictionary的组合? - How would I implement a QueueDictionary, a combination of Queue and Dictionary in C#? C#中的“详细词典”,“覆盖新”这个[]或实现IDictionary - “Verbose Dictionary” in C#, 'override new' this[] or implement IDictionary 在字典 C# 中使用 LinkedList? 以及如何实施它的操作? - Using LinkedList in Dictionary C#? And how to implement it's operations? 如何使用字典在C#中实现密码验证程序 - how can I implement a password validation program in C# with a dictionary 为什么C#Dictionary没有实现所有的IDictionary? - Why doesn't the C# Dictionary implement all of IDictionary? C#:如何使用包含具体值的具体字典中的接口值的 IReadOnly 字典实现接口 - C#: How to implement an interface with an IReadOnly dictionary containing interface values from a concrete dictionary containing concrete values C#字典词典查询 - c# `dictionary of dictionary` query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM