将相似的方法合并为一个方法以减少重复

本文关键字:方法 一个 相似 合并 | 更新日期: 2023-09-27 18:01:58

我正在努力学习和实践面向对象的原则,我需要一些例子来帮助我克服困难。我有以下代码:

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;
        }
    }
}

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

将相似的方法合并为一个方法以减少重复

你可以试试这样做,但我不认为它有太大帮助:

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();
}

在。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;
        }
    }
}