将类似的方法合并到单个方法中以减少重复

时间:2015-07-09 05:05:51

标签: c# oop methods coding-efficiency

我正在努力学习和练习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;
        }
    }
}

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

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:-1)

在.net everthing中基于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;
        }

    }
}