-1

我正在努力学习和实践 OOP 原则,我需要一些帮助来帮助我克服困难。我有以下代码:

using System.Collections.Generic;
namespace Test
{
    class Program
    {
        static void Main()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();

            dictionary.Add("cat", "one");
            dictionary.Add("dog", "two");
            dictionary.Add("llama", "three");
            dictionary.Add("iguana", "four");

            var test1 = GetKVP(dictionary, "llama");
            var test2 = GetValue(dictionary, "llama");
            var test3 = GetPosition(dictionary, "llama");
        }

        static KeyValuePair<string, string> GetKVP(Dictionary<string, string> dict, string key_to_find)
        {
            foreach (KeyValuePair<string, string> kvp in dict)
            {
                if (kvp.Key == key_to_find)
                {return kvp;}
            }
            return new KeyValuePair<string, string>();
        }

        static string GetValue(Dictionary<string, string> dict, string key_to_find)
        {
            foreach (KeyValuePair<string, string> kvp in dict)
            {
                if (kvp.Key == key_to_find)
                {return kvp.Value;}
            }
            return string.Empty;
        }

        static int GetPosition(Dictionary<string, string> dict, string key_to_find)
        {
            int counter = 0;
            foreach (KeyValuePair<string, string> kvp in dict)
            {
                if (kvp.Key == key_to_find)
                {return counter;}
                counter += 1;
            }
            return -1;
        }
    }
}

我想要做的是整合代码集,这样我就可以有一个方法来返回不同的数据类型而无需重复代码。 请不要评论有几种更有效的方法来搜索字典这一事实,我知道这并不理想。我只是模拟了一些数据和方法以用作示例。对于我的生活,我真的无法想象如何实现这样的东西。

4

2 回答 2

0

您可以尝试这样做,但我认为它没有太大帮助:

static R GetResult<R>(Dictionary<string, string> dict, string key_to_find, Func<KeyValuePair<string, string>, R> selector, R otherwise)
{
    return dict.Where(kvp => kvp.Key == key_to_find).Select(kvp => selector(kvp)).DefaultIfEmpty(otherwise).First();
}

static KeyValuePair<string, string> GetKVP(Dictionary<string, string> dict, string key_to_find)
{
    return GetResult(dict, key_to_find, kvp => kvp, new KeyValuePair<string, string>());
}

static string GetValue(Dictionary<string, string> dict, string key_to_find)
{
    return GetResult(dict, key_to_find, kvp => kvp.Value, String.Empty);
}

static int GetPosition(Dictionary<string, string> dict, string key_to_find)
{
    return dict.Where(kvp => kvp.Key == key_to_find).Select((kvp, n) => n).DefaultIfEmpty(-1).First();
}
于 2015-07-09T05:40:08.260 回答
-1

在 .net 中,一切都基于 Object,所以只需返回 Object,然后 object 可以是任何你想要的东西

这是基于您的代码的示例

using System.Collections.Generic;
namespace Test
{
    class Program
    {
        static void Main()
        {
            Dictionary<string, Object> dictionary = new Dictionary<string, string>();

            dictionary.Add("cat", "one");
            dictionary.Add("dog", "two");
            dictionary.Add("llama", "three");
            dictionary.Add("iguana", "four");

            var test1 = GetWhatEver(dictionary, "llama");
            var test2 = GetWhatEver(dictionary, "llama");
            var test3 = GetWhatEver(dictionary, "llama");
        }

         static Object GetWhatEver(Dictionary<string, Object> dict, string key_to_find)
        {
            foreach (var kvp in dict)  
            {
                if (kvp.Key == key_to_find)
                {return kvp.Value;}
            }
            return null;
        }

    }
}
于 2015-07-09T05:46:36.513 回答